Have your say in Community Polls: What was/is your greatest motivation to start your own business?
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.

How can we retrieve the ID of a user created Cart within a Theme App Extension?

How can we retrieve the ID of a user created Cart within a Theme App Extension?

triplesingle
Shopify Partner
22 0 1

For more background, I want to update a Cart Attribute within my Theme App Extension with a GraphQL Mutation. This Mutation accepts the Cart ID as a param but I'm not sure how to get the cart ID of current user.

Reply 1 (1)

Huptech-Web
Shopify Partner
1010 204 217

Hello @triplesingle 

You can get the Current user Cart ID in the theme extension by shopify cookies for that use the code below.

 

 

// 1. function for getting cookies value
const getCookie = cname => {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
};
// 2. Get the current user cart ID 
const cartID=getCookie("cart")?`gid://shopify/Cart/${getCookie("cart")}`:""

 

 

You get cartId = gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSFhSODRYMERHNVo3WlRQMVI1UUZLMU45 (for example )

use this ID in your graphQl Mutition or query 

 

 

If the solution presented meets your needs and addresses your query effectively, I encourage you to accept it as the chosen answer. This will acknowledge the support you received and aid fellow community members in identifying reliable and effective solutions for their similar concerns.
Thank you.

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required