After following this tutorial for adding a discount function in Shopify through functions and building a UI in Remix: Build a discounts UI with Remix. I tested this particular block of code on two different stores with two different results:
export const action = async ({ params, request }) => {
const { functionId } = params;
const { admin } = await shopify.authenticate.admin(request);
const formData = await request.formData();
const {
title,
method,
code,
combinesWith,
usageLimit,
appliesOncePerCustomer,
startsAt,
endsAt,
configuration,
} = JSON.parse(formData.get("discount"));
const baseDiscount = {
functionId,
title,
combinesWith,
startsAt: new Date(startsAt),
endsAt: endsAt && new Date(endsAt),
};
const metafields = [
{
namespace: "$app:volume-discount",
key: "function-configuration",
type: "json",
value: JSON.stringify({
quantity: configuration.quantity,
percentage: configuration.percentage,
}),
},
];
if (method === DiscountMethod.Code) {
const baseCodeDiscount = {
...baseDiscount,
title: code,
code,
usageLimit,
appliesOncePerCustomer,
};
const response = await admin.graphql(
`#graphql
mutation CreateCodeDiscount($discount: DiscountCodeAppInput!) {
discountCreate: discountCodeAppCreate(codeAppDiscount: $discount) {
codeAppDiscount{
discountId
}
userErrors {
code
message
field
}
}
}`,
{
variables: {
discount: {
...baseCodeDiscount,
metafields,
},
},
},
);
const responseJson = await response.json();
const errors = responseJson.data.discountCreate?.userErrors;
const discount = responseJson.data.discountCreate?.codeAppDiscount;
return json({ errors, discount: { ...discount, functionId } });
} else {
const response = await admin.graphql(
`#graphql
mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) {
discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) {
automaticAppDiscount {
discountId
}
userErrors {
code
message
field
}
}
}`,
{
variables: {
discount: {
...baseDiscount,
metafields,
},
},
},
);
const responseJson = await response.json();
const errors = responseJson.data.discountCreate?.userErrors;
return json({ errors });
}
};
On my old development store created about 2 - 3 years ago, the following error is returned when calling the discountAutomaticAppCreate
mutation:
{
"data": {
"discountCreate": {
"automaticAppDiscount": null,
"userErrors": [
{
"code": "INVALID",
"message": "Function not found.",
"field": [
"automaticAppDiscount",
"functionId"
]
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1990,
"restoreRate": 100
}
}
}
}
This error is returned even though the function ID is valid and all the suggestions have been followed according to this previous thread with similar errors: Solved: GraphQL can't create Production Function though it can find it - Shopify Community (I can’t link to other threads because Shopify prevents me from adding more than 2 links)
When the same code in Remix is called for a Shopify development store that was created about 1 - 2 weeks ago no error response is returned and a discount is successfully created.
Is this a bug on Shopify’s platform? Do older development stores have no or poor support for newer features provided in Shopify’s APIs?