Hi Guys I am doing an app with remix for discounts, all is going good and I am coding a DataTable where the merchant can edit the discount, when the discount is clicked it takes the merchant to the specific page successfully, but I dont know how to query the metafields for that specific discount dynamically so it works everywhere.
Here is what i have so far in my query:
export async function loader({ request }) {
const { admin } = await shopify.authenticate.admin(request);
const discountCodeId = gid://shopify/DiscountAutomaticNode/1095825490072;
const response = await admin.graphql({ discountNode(id: "${discountCodeId}") { id metafield(namespace: "volume-discount", key:"function-configuration"){ value } discount { ... on DiscountCodeBasic { title } ... on DiscountCodeBxgy { title } ... on DiscountCodeFreeShipping { title } ... on DiscountAutomaticApp { title } ... on DiscountAutomaticBasic { title } ... on DiscountAutomaticBxgy { title } ... on DiscountAutomaticFreeShipping { title } } } });
const parsedResponse = await response.json();
return json({
discount: parsedResponse.data,
});
}
and this is how I create the it:
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),
};
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) { userErrors { code message field } } },
{
variables: {
discount: {
…baseCodeDiscount,
metafields: [
{
namespace: “$app:volume-discount”,
key: “function-configuration”,
type: “json”,
value: JSON.stringify({
quantity: configuration.quantity,
percentage: configuration.percentage,
bundles: configuration.bundles
}),
},
],
},
},
}
);
const responseJson = await response.json();
const errors = responseJson.data.discountCreate?.userErrors;
return json({ errors });
} else {
const response = await admin.graphql(
#graphql mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) { discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) { userErrors { code message field } } },
{
variables: {
discount: {
…baseDiscount,
metafields: [
{
namespace: “$app:volume-discount”,
key: “function-configuration”,
type: “json”,
value: JSON.stringify({
quantity: configuration.quantity,
percentage: configuration.percentage,
bundles: configuration.bundles
}),
},
],
},
},
}
);
const responseJson = await response.json();
const errors = responseJson.data.discountCreate?.userErrors;
return json({ errors });
}
};