Headless commerce and custom storefronts with Shopify APIs and SDKs
Im making a private App that applies a discount to the Checkout.
Im have to use the Stroefront API since private Apps cant get
var discountUrl = TestStoreURL + '/api/graphql.json';
var base64Id = Buffer.from(id.toString()).toString('base64');
const checkoutQuery = `
mutation checkoutDiscountCodeApplyV2($discountCode: String!, $checkoutId: ID!) {
checkoutDiscountCodeApplyV2(discountCode: $discountCode, checkoutId: $checkoutId) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
}
{
checkoutId: ` + base64Id + `,
discountCode: ` + discountCode + `
}
`;
const results = await fetch(discountUrl, {
method: 'POST',
uri: discountUrl,
headers : {
'X-Shopify-Storefront-Access-Token' : SHOPIFY_ACCESS_STOREFRONT_TOKEN_TEST,
'Accept': 'application/json, text/plain, ',
'Content-Type': 'application/json'
},
body : JSON.stringify(checkoutQuery)
})
.then(response => response.json())
.then(json => {
log.info(json);
});
permisson for the Checkout scope of the Admin API.
So im trying to use the checkoutDiscountCodeApplyV2 mutation. But im always getting errors: 'Parameter Missing or Invalid' and no futher information.
Here is the relevant code:
You likely need to pass the variables and query separately when using fetch.
const checkoutQuery = `
mutation checkoutDiscountCodeApplyV2($discountCode: String!, $checkoutId: ID!) {
checkoutDiscountCodeApplyV2(discountCode: $discountCode, checkoutId: $checkoutId) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
}
`;
body: JSON.stringify({
query: checkoutQuery,
variables: {
checkoutId: base64Id,
discountCode
}
})
Thanks for your reply I changed my code according to your suggestion and the variables are now send correctly.
Im currently sending the checkoutId as such:
Buffer.from('gid://shopify/Checkout/'+id.toString()).toString('base64');
But i get the following Error as a response:
{
code: 'INVALID',
field: [ 'checkoutId' ],
message: 'Checkout existiert nicht'
}
And i get the checkout if directly from the ctx.request.body.id.
Do you might know why i cant find the checkout?.
Do i hve to use the Checkout update instead of the Checkout creation webhook?
Thanks to all who participated in our AMA with 2H Media on planning your 2023 marketing bu...
By Jacqui Mar 30, 2023Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023