Need to get the tags of the product from the cart

I want to get the tags associated with the products in the cart , basically from the cart API.
But it doesnt return the tags for some reason (query optimizations). Is there any way to do that since i need to get the product tags to a fucntion im implementing.

query RunInput {
cart {
deliveryGroups {
deliveryOptions {
handle
title
}
}
lines {
merchandise {
… on ProductVariant {
product {
id
tags(CANT GET THIS)
}
}
}
}
}
}

You should be able to get that as how you were doing. I went ahead and tried in all currently supported API versions and all of them works with below query. Maybe the variables were missing in your case.

You can try the below query by just replacing CART_ID.

query RunInput {
  cart(id: "gid://shopify/Cart/CART_ID") {
    deliveryGroups(first: 10) {
      edges {
        node {
          deliveryOptions {
          	handle
          	title
          }
        }
      }
    }
    lines(first: 100) {
      edges {
        node {
          merchandise {
            ... on ProductVariant {
              product {
                id
                tags
              }
            }
          }
        }
      }
    }
  }
}

If this doesn’t work still, it would be helpful to provide details listed below:

  • API version that you are using
  • Exact query and variables
  • Returning response
  • How you create a cart

You can also install this GraphiQL explorer in your store to try queries there.

Hi ,

The issue is, its not for an already created order, Im trying to execute the function when the user is in the checkout.
However when I run I get this error :

[FAILED] GraphQL Document Validation failed with 1 errors;
[FAILED] Error 0: Cannot query field “tags” on type “Product”.

Hi!

I see now. You can check the RunInput from docs, if you didn’t already.

It seems like tags are not supported in the returned response for RunInput object but you can use hasAnyTag or hasTags if you know the exact tag name. For more, you can check this document in Shopify.

This is how we are using on our end:

query RunInput($allTags: [String!]!) {
  cart {
    lines {
      quantity
      cost {
        amountPerQuantity {
          amount
        }
      }
      merchandise {
        ... on ProductVariant {
          id
          product {
            hasTags(tags: $allTags) {
              tag
              hasTag
            }
          }
        }
      }
    }
  }
}

I hope this helps!

Thanks alot! Yes mate, this is correct if you know the exact tag, but if its a dynamic tag like a date we cant query it rite. I think there is no way they gonna send all the tags in that query.

1 Like