skip to Main Content

I am useing shopify Storefront API to create an ecommerce website. When i create a cart with cartCreate mutation, i get an cart id, the graphql mutation is given below:

 mutation createCart($input: CartInput!) {
    cartCreate(input: $input) {
      cart {
        id
      }
    }
  }

I can use the cart id to get the cart details. the graphql guery is given below:

query {
  cart(
    id: "gid://shopify/Cart/1"
  ) {
    id
    createdAt
    updatedAt
    lines(first: 10) {
      edges {
        node {
          id
          quantity
          merchandise {
            ... on ProductVariant {
              id
            }
          }
          attributes {
            key
            value
          }
        }
      }
    }
    attributes {
      key
      value
    }
    cost {
      totalAmount {
        amount
        currencyCode
      }
    }
    buyerIdentity {
      email
      phone
    }
  }
}

but the problem is, when a user logout or change device then there is any way to get the cart ID using a Customer token or other way, sothat i am able to get the cart details.

2

Answers


  1. Once the user is authenticated, you can use the Storefront API’s customer query to fetch the customer’s cart information. Specifically, you’ll need to query for the id of the cart. Here’s an example GraphQL query,
    (Replace "YOUR_CUSTOMER_ACCESS_TOKEN" with the actual customer access token of the logged-in user):

    query {
      customer(customerAccessToken: "YOUR_CUSTOMER_ACCESS_TOKEN") {
        id
        email
        cart {
          id
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search