What's your biggest current challenge? Have your say in Community Polls along the right column.
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 long checkout object exists?

Solved

How long checkout object exists?

paulcooksome
Shopify Partner
2 0 0

I'd like to ask how long the checkout object created with Storefront API exists.

 

I'd like to create checkout object using storefront API and keep the object ID in my localstorage

My goal is to get total price where discount code is applied.

To do this, I create checkout object, add line items there and apply discount. So three GraphQL calls are being made, which are too slow to get final price.

 

Is it possible to create checkout object only once per web session and keep in localStorage?

 

Accepted Solution (1)

SBD_
Shopify Staff
1831 273 421

This is an accepted solution.

Hey @paulcooksome 

 



To do this, I create checkout object, add line items there and apply discount. So three GraphQL calls are being made, which are too slow to get final price.

You should be able to do this with a single call:

 

mutation createCart($cartInput: CartInput) {
  cartCreate(input: $cartInput) {
    cart {
      id
      checkoutUrl
      lines(first: 10) {
        edges {
          node {
            id
            merchandise {
              ... on ProductVariant {
                id
              }
            }
          }
        }
      }
      cost {
        totalAmount {
          amount
          currencyCode
        }
      }
    }
  }
}

Variables:

{
  "cartInput": {
    "lines": [{...}],
    "discountCodes": ["code"]
  }
}

 


It's ok to keep the cart ID in local storage, but given the cart might change (discounts, line items, etc) it's probably best to look up the cart each time to confirm prices and retrieve the checkout URL.

I believe checkouts live for ~3 months.

Scott | Developer Advocate @ Shopify 

View solution in original post

Reply 1 (1)

SBD_
Shopify Staff
1831 273 421

This is an accepted solution.

Hey @paulcooksome 

 



To do this, I create checkout object, add line items there and apply discount. So three GraphQL calls are being made, which are too slow to get final price.

You should be able to do this with a single call:

 

mutation createCart($cartInput: CartInput) {
  cartCreate(input: $cartInput) {
    cart {
      id
      checkoutUrl
      lines(first: 10) {
        edges {
          node {
            id
            merchandise {
              ... on ProductVariant {
                id
              }
            }
          }
        }
      }
      cost {
        totalAmount {
          amount
          currencyCode
        }
      }
    }
  }
}

Variables:

{
  "cartInput": {
    "lines": [{...}],
    "discountCodes": ["code"]
  }
}

 


It's ok to keep the cart ID in local storage, but given the cart might change (discounts, line items, etc) it's probably best to look up the cart each time to confirm prices and retrieve the checkout URL.

I believe checkouts live for ~3 months.

Scott | Developer Advocate @ Shopify