I had AI reword my reply a little more technical since I am in a little bit of a rush, but this should be a little more clear:
In my Shopify Product Discount Function, I dynamically pass tags and collection IDs to apply discounts based on customer tags and product collections. Here’s how the process works using GraphQL queries and Shopify Metafields:
1. GraphQL Query (graphql.run)
I use the following GraphQL query to retrieve information about the products in the cart, as well as the tags associated with the customer:
query Input($selectedCollectionIds: [ID!], $tags: [String!]) {
cart {
lines {
merchandise {
__typename
... on ProductVariant {
id
product {
handle
inCollections(ids: $selectedCollectionIds) {
collectionId
isMember
}
}
}
}
}
buyerIdentity {
customer {
hasTags(tags: $tags) {
tag
hasTag
}
}
}
}
shop {
metafield(namespace: "simplewholesale", key: "tags") {
value
}
}
}
Breakdown:- inCollections(ids: $selectedCollectionIds): This checks whether the products in the cart belong to any of the collections passed in the $selectedCollectionIds variable.
- hasTags(tags: $tags): This checks if the customer has any of the tags specified in the $tags variable, which I pass to the function.
2. Dynamic Content Storage in Metafields
The dynamic values for the tags and collections used in the query are stored in a Shopify Metafield. Here’s an example of how I set the metafield using a GraphQL mutation:
mutation SetMetafield {
metafieldsSet(metafields: [
{
namespace: "$app:my-namespace",
key: "my-key",
ownerId: "OWNER_ID",
type: "json",
value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/310827712556\"],\"tags\":[\"Pro\",\"VIP\"]}"
}
]) {
metafields {
id
}
}
}
Metafield Structure:
The metafield stores the collections and tags in a JSON structure like this:
{
"selectedCollectionIds": [
"gid://shopify/Collection/310827712556"
],
"tags": [
"Pro",
"VIP"
]
}
This JSON object is used to dynamically manage discount logic. The selectedCollectionIds array contains collection IDs that the product might belong to, and the tags array stores customer tags (like “Pro” and “VIP”).
3. Querying Metafield Data in run.graphql
In my run.graphql query, I pass variables from the metafield data to dynamically check for product collections and customer tags. Specifically:
-
inCollections(ids: $selectedCollectionIds): This checks whether products in the cart are part of any of the collections in the selectedCollectionIds array from the metafield.
-
hasTags(tags: $tags): This checks if the customer has any of the tags specified in the tags array from the metafield.
Summary:
In this setup, the dynamic content (like collection IDs and customer tags) is stored in a metafield as a JSON object. I use that metafield to pass values to the GraphQL query for determining which collections a product belongs to and which tags a customer has. This allows the discount function to apply discounts based on the customer’s tags and the product’s collection membership.
This structure makes it easy to dynamically update the tags and collection IDs in the metafield without modifying the core function logic, ensuring flexibility in applying discounts.