App reviews, troubleshooting, and recommendations
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:
Hi @jacobovelasquez,
I have created a sample code.
If there is anything here that’s unclear please do let me know!
app/routes/app.discount.$functionId.$id.tsx
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
const { admin } = await shopify.authenticate.admin(request);
const { id } = params;
const discountNode = await getDiscount(admin, id!);
return json({ discountNode });
};
const getDiscount = async (
admin: AdminApiContext<RestResources>,
id: string,
) => {
const response = await admin.graphql(
getDiscountNode(),
{
variables: {
id: 'gid://shopify/DiscountNode/' + id,
namespace: METAFIELD_NAMESPACE,
key: METAFIELD_CONFIGURATION_KEY
},
}
);
const responseJson = await response.json();
const discountNode = responseJson.data.discountNode;
if (!discountNode) return undefined;
if (!discountNode.metafield) return undefined;
if (typeof discountNode.metafield.value !== 'object') {
const value = JSON.parse(discountNode.metafield.value)
value.volume = value.volume.map((obj: any) => ({
quantity: String(obj.quantity),
percentage: String(obj.percentage)
}));
discountNode.metafield.value = value;
}
return discountNode;
}
const getDiscountNode = () => {
return `#graphql
query getDiscountNode($id: ID!, $namespace: String!, $key: String!){
discountNode(id: $id) {
discount {
... on DiscountAutomaticApp {
__typename
asyncUsageCount
combinesWith {
orderDiscounts
productDiscounts
shippingDiscounts
}
endsAt
startsAt
status
title
}
... on DiscountCodeApp {
__typename
asyncUsageCount
appliesOncePerCustomer
codeCount
combinesWith {
orderDiscounts
productDiscounts
shippingDiscounts
}
endsAt
startsAt
status
title
usageLimit
codes(first: 1) {
edges {
node {
code
}
}
}
}
}
id
metafield(namespace: $namespace, key: $key) {
value
id
}
}
}`
}
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024