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 to Dynamically Query Shopify Product Tags Stored in Metafields Using GraphQL?

How to Dynamically Query Shopify Product Tags Stored in Metafields Using GraphQL?

kristi01
Shopify Partner
6 0 0

 

Hi everyone,

I'm currently working on Shopify function where I need to dynamically replace a static tag name in a GraphQL query with values stored in metafields. For example, in the code below, I'm checking the product tag statically ("winter-sale") using the hasAnyTag function. However, I want to check this tag dynamically using a metafield value instead.

I am using the Remix template to create a discount function and querying the above code in run.graphql.

 

query RunInput {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
              id,
              tags,
              hasAnyTag(tags: ["winter-sale"]),
            }
        }
      }
    }
  }
  discountNode {
    metafield(namespace: "$app:shopify-discount-new", key: "shopify-configuration-new") {
      value
    }
  }
}

 


Here is the code for creating a discount and storing values in a metafield.

 

const response = await admin.graphql(
      `#graphql
          mutation CreateCodeDiscount($discount: DiscountCodeAppInput!) {
            discountCreate: discountCodeAppCreate(codeAppDiscount: $discount) {
              codeAppDiscount{
                discountId
              }
              userErrors {
                code
                message
                field
              }
            }
          }`,
      {
        variables: {
          discount: {
            ...baseCodeDiscount,
            metafields: [
              {
                namespace: "$app:shopify-discount-new",
                key: "shopify-configuration-new",
                type: "json",
                value: JSON.stringify({
                  quantity: configuration.quantity,
                  percentage: configuration.percentage,
                  tag: configuration.tag
                }),
              },
            ],
          },
        },
      },
    );

 

 

Could someone help me figure out how to replace the static tag ("winter-sale") with a value retrieved from a metafield?

Replies 0 (0)