I am trying to add a shopify function to our store and its functionality should be to give free shipping to customers that have at least one of these two tags: "swell_vip_learned lounger", "swell_vip_pj professional".
I have created a custom app and added an extension of type "shipping-discount". The app is installed successfully and discount is also showing applied. but when I open the checkout as a customer that has those tags, it donot apply free shipping. I am providing code for my run.graphql and run.js both as only these are two main files in extension. Kindly help me what could be wrong here ?
run.grpahql
query RunInput {
cart {
deliveryGroups{
id
}
buyerIdentity {
customer {
hasTags(tags: ["swell_vip_learned lounger", "swell_vip_pj professional"]) {
hasTag
tag
}
}
}
cost {
subtotalAmount {
amount
}
}
}
}
run.js
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
*/
/**
* @type {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
discounts: [],
};
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
console.log("Free Shipping Validation Function");
// fetch("https://haroon-ins-dev.free.beeceptor.com/", {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(input) } )
const headers = new Headers();
headers.append('Authorization', 'Bearer SOME-VALUE');
fetch('https://haroon-ins-dev.free.beeceptor.com', {
method: 'POST',
headers: headers,
body: JSON.stringify(input)
})
.then(response => response.text())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
const SWELL_TIER_2 = "swell_vip_learned lounger";
const SWELL_TIER_3 = "swell_vip_pj professional";
let customer = input.cart.buyerIdentity.customer;
const deliveryGroupId = input.cart.deliveryGroups.id;
if (
// customer has tags
customer && (
customer?.hasTags.find((t) => t.tag === SWELL_TIER_2)?.hasTag === true ||
customer?.hasTags.find((t) => t.tag === SWELL_TIER_3)?.hasTag === true
)
) {
// return free shipping
const ShippingDiscount = {
discounts: [
{
message: "Free VIP Shipping",
targets: {
deliveryGroup: {
id: deliveryGroupId,
},
},
value: {
percentage: 100,
},
},
],
};
return ShippingDiscount;
} else {
// otherwise return empty discount (no free shipping)
return EMPTY_DISCOUNT;
}
}
I tried adding a console.log to show what form of data I am getting inside input but it donot show in the console of frontend.
Tried doing a post request to Beeceptor but request is not reaching there as well.
2
Answers
I fixed the issue with the help provided by Simarjeet Singh and some additional fixes. Here are the fixes I applied that might help anyone seeking answer to same question.
The code you provided seems to be on the right track, but there are a couple of issues that might be preventing free shipping from applying correctly. Let’s break it down:
1. Logging Issues:
2. Free Shipping Logic:
The logic to check for tags and apply the discount looks good. However, consider these points:
customer?.hasTags.find
considers case sensitivity. Shopify tags might be case-sensitive. You can usetoLowerCase()
to ensure a match regardless of case.hasTag
properties are true within thefind
function.3. Debugging Tips:
customer.hasTags
within the function to verify it retrieves the customer tags correctly.Here’s an improved version of
run.js
incorporating the suggestions: