New Shopify Certification now available: Liquid Storefronts for Theme Developers

How do I grab a cart without a cartId?

Liquidator3358
Explorer
43 1 12

Can someone please explain to me how I can grab the user's cart without having the cartId?  Surely there is a way to grab the cart using the user's credentials and/or accessToken?

 

You can grab a customer without their id like so:

 

 

 

 const queryCust = `{
            customer(customerAccessToken: "${token}"){
              id
              firstName
              lastName
              defaultAddress{
                address1
                zip
              }
            }
          }
    `

      const custData = await shopifyFetch(queryCust)
      setUserData(custData)
  }

 

 

This works perfectly fine and is very intuitive.

 

 Shouldn't I be able to do the same with a cart like this?

 

 

 cart(customerAccessToken: "${token)"{
            id
            createdAt
                lines(first:10) {
                  edges {
                    node {
                      quantity
                      merchandise {
                        ... on ProductVariant {
                          title
                          priceV2 {
                            amount
                            currencyCode
                          }
                          product {
                            title
                          }
                        }
                      }
                    }
                  }
                }
          }
      }
    `
        const data = await shopifyFetch(queryItems)
        setCartData(data)

 

 

Or is there a better way to do this?  I can easily persist the cartId through localStorage or some other caching layer (redis) but I guess I am confused as to why you can call a customer by their token but not their associated cart?  

 

When a user logs out, that cartId SHOULD disappear on the client side.  I see zero reason to keep it in localStorage (the same applies to their accessToken).  When they log back on, you generate a new accessToken and then fetch the cart through that token.

 

Does it work differently than this?

Replies 0 (0)