Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How to get cart id of a cart if I didn't create the cart?

How to get cart id of a cart if I didn't create the cart?

jeffstaq
Shopify Partner
7 0 2

I'm working on a shopify extension and I'm trying to add some meta fields to the cart through the storefront graphql api. I need cart id to do so, but there is no way to get the cart id, the only query to get the cart requires the cart id. How does one get the cart id for use in the storefront api if they didn't create the cart? 

Replies 4 (4)

Ugurcan
Shopify Partner
351 7 34

you can check window.Shopify on storefront and see what is available.

If there is no cart id there, you cannot get it.

Pasilobus | eCommerce Experts, Shopify Apps & Development since 2015 -- www.pasilobus.com

theaisyaaziz
Shopify Partner
14 1 6

Instead of a cart id, you should be able to get the checkout token from the extension: https://shopify.dev/docs/api/checkout-ui-extensions/unstable/apis/checkout-token

 

Instead of doing it via the Storefront API, you can also add a metafield via the extension itself: https://shopify.dev/docs/api/checkout-ui-extensions/unstable/apis/metafields (use the useApplyMetafieldsChange() API)

ndt_ts
Shopify Partner
27 3 8

But are cart Id and checkout token same? can we use checkout token in places where we need to use cart id like in this mutation: https://shopify.dev/docs/api/storefront/2023-10/mutations/cartattributesupdate ?

theaisyaaziz
Shopify Partner
14 1 6

AFAIK, the only way you can access the cart id is if you create them.

 

But another way you can do this is via the Metafields mutation in the Checkout UI Extension: https://shopify.dev/docs/api/checkout-ui-extensions/unstable/apis/metafields

 

Specifically, you can use:

useApplyMetafieldsChange()
and use type: updateCartMetafield