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?
As a business owner, have you ever wondered when your customer's first impression of yo...
By Skye Jun 6, 2023We're excited to announce improvements to the threaded messaging experience in our communi...
By TyW May 31, 2023Thank you to everyone who participated in our AMA with Klaviyo. It was great to see so man...
By Jacqui May 30, 2023