I have created a Shopify Function that applies discounts based on the product collection. The input variable inAnyCollection is always getting false even if the products are in the selected collection.
Here is the graphql code I used to create the meta field
const collections = {
selectedCollectionIds: [
'gid://shopify/Collection/471602364xxx',
'gid://shopify/Collection/471602397xxx',
'gid://shopify/Collection/435940852xxx',
'gid://shopify/Collection/471602331xxx',
],
};
const input = {
metafields: [
{
key: 'sq-discount-input',
namespace: 'sq-collection',
ownerId: "gid://shopify/DiscountCodeNode/1420419760xxx",
type: 'json',
value: JSON.stringify(collections),
},
],
};
const CREATE_METAFIELD_SET_QUERY = `
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
}
userErrors {
field
message
}
}
}
`;
const metafieldSetResponse = await client.query({
data: {
query: CREATE_METAFIELD_SET_QUERY,
variables: input,
},
});
Here is my run.graphql
query RunInput($selectedCollectionIds: [ID!]) {
cart {
lines {
quantity
merchandise {
__typename
... on ProductVariant {
id
product {
id
inAnyCollection(ids: $selectedCollectionIds)
}
}
}
}
}
discountNode {
metafield(namespace: "$app:product-discount", key: "sq-discount-configuration") {
value
}
}
}
Here is my shopify.extension.toml file
api_version = "2024-01"
[[extensions]]
handle = "product-discount"
name = "t:name"
description = "t:description"
type = "function"
[[extensions.targeting]]
target = "purchase.product-discount.run"
input_query = "src/run.graphql"
export = "run"
[extensions.build]
command = ""
path = "dist/function.wasm"
[extensions.ui.paths]
create = "/"
details = "/"
[extensions.input.variables]
namespace = "sq-discount-input"
key = "sq-collection"
Here are the function run details am getting from Shopify
{
"cart": {
"lines": [
{
"quantity": 1,
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/444368989719xx",
"product": {
"id": "gid://shopify/Product/8119129145xxx",
"inAnyCollection": false
}
}
}
]
},
"discountNode": {
"metafield": {
"value": "{\"amount\":\"99\",\"quantity\":1}"
}
}
}
Here is the version of shopify cli
"@shopify/app": "^3.34.0",
"@shopify/cli": "^3.34.0",
Thank you for your suggestions and help.