How to fix Cannot find cart by ownership code?

PROBLEM:
Hi, i’m using createCheckoutFromCurrentCart(), in reference to docs: Create Checkout From Current Cart | Velo

And i’m getting this error message, can you please tell me how to solve this problem, am i missing something in the checkout options?

Message
message: 'Cart not found: Cannot find cart by ownership'
details:
applicationError:
description: Cannot find cart by ownership
code: OWNED_CART_NOT_FOUND
data: {}

backend/createCheckout.web.js

import { currentCart } from "wix-ecom-backend";

//Creating checkout ID From the current cart
export const myCreateCheckoutFromCurrentCartFunction = webMethod(
  Permissions.Anyone,
  async (options) => {
    try {
      const checkoutId =
        await currentCart.createCheckoutFromCurrentCart(options);

      return checkoutId;
    } catch (error) {
      console.error(error);
      // Handle the error
    }
  },
);

Page code

import { myCreateCheckoutFromCurrentCartFunction } from "backend/createCheckout.web.js";

  //Checkout options 
    const checkoutOptions = {
        // channelType is a required field
        billingAddress: order.description.billingAddress,
        channelType: "WEB",
        email: order.description.shippingAddress.email || order.description.billingAddress.email,
        shippingAddress: order.description.shippingAddress,
    };

    //Create checkout from current cart

    myCreateCheckoutFromCurrentCartFunction(checkoutOptions)
        .then((checkoutId) => {
            console.log("Success! Checkout created, checkoutId:", checkoutId);

        })
        .catch((error) => {
            console.error(error);
            // Handle the error
        });

Are there any red lines in your code?

I don’t see this line anywhere in your backend code:

import { Permissions, webMethod } from "wix-web-module";

Web methods won’t run without importing this.


Where are you fetching this from? An order is placed after checking out, correct? Also fetching order info requires elevated permissions.

This is what I think might be the cause of this error.


My suggestions would be to:

  1. Test it out (while logged out) without passing any Email ID or shipping / billing addresses.
  2. If that does not work, try logging in and test it out.
  3. If it works, then all you have to do is ask the user to log in to proceed to checkout, else keep your custom checkout button hidden.
  4. If you don’t want visitors to log in (something like a guest checkout), you will still need to ask them for their email ID and shipping / billing addresses as these fields are mentioned as required in order to create a checkout if the user is not logged in. Once the user fills up this required info, you should be able to create the checkout successfully.