Skip to content

How can I access cart to determine whether product is already in cart? #2531

Answered by w3nda
w3nda asked this question in Help
Discussion options

You must be logged in to vote

I ended up using this approach but changing the way I fetch the cart so leaving this here for whoever needs it.
Why would I need it?

  1. You want to access the cart from anywhere
  2. You want to listen to it's changes

Steps

  1. Start with moving the cart from loadDeferredData to loadCriticalData.
  2. Make sure you await cart.get and return it, that way you will always have access to the data without the need to await the promise.
    It should look like this:
async function loadCriticalData({context}: LoaderFunctionArgs) {
  const {storefront, cart} = context;

  const [header] = await Promise.all([
    storefront.query(HEADER_QUERY, {
      cache: storefront.CacheLong(),
      variables: {
        heade…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@w3nda
Comment options

@w3nda
Comment options

Answer selected by w3nda
@wizardlyhel
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants