Hello,
I developed a discount app for an merchant. In my discount function I added logic that discounts with same prefix as a discount already applied get rejected. The discount function holds automatic discounts and runs with every discount change.
In my dev store (plus store without preview) its working fine. When I enter the second discount with the same prefix it gets not applied and the rejection message is shown.
But on the live store of the merchant the second discount gets applied AND the rejection message is shown. I then can proceed checkout with both discounts applied.
When I hit F5 and refresh the checkout page there is only one discount shown, so my rejection gets processed only then.
I noticed as well that in the logs the triggering Discount code always shows null (“triggeringDiscountCode”: null). But even without using that and just sorting the discounts to always reject the same its not working.
Edit:
Thats my actual approach in the beginning of my cart_lines_discounts_generate_run.ts:
// --- Discount code validations (prefix may only be used once) ---
const operations: any[] = [];
const entered = input.enteredDiscountCodes ?? [];
if (entered.length > 0) {
const shaped = entered
.map((c) => ({ code: c.code, rejectable: c.rejectable }))
.filter((c) => isCodeWith8CharSuffix(c.code));
if (shaped.length > 1) {
const shapedSorted = [...shaped].sort((a, b) => a.code.localeCompare(b.code));
// group by prefix
const byPrefix = new Map<string, { code: string; rejectable: boolean }[]>();
for (const c of shapedSorted) {
const p = codePrefix(c.code);
const arr = byPrefix.get(p) ?? [];
arr.push(c);
byPrefix.set(p, arr);
}
// reject duplicates (keep first per prefix)
for (const [prefix, list] of byPrefix) {
if (list.length <= 1) continue;
const rejectCodes = list
.slice(1)
.filter((c) => c.rejectable)
.map((c) => ({ code: c.code }));
if (rejectCodes.length) {
operations.push({
enteredDiscountCodesReject: {
codes: rejectCodes,
message: `Es darf nur ein Rabattcode ${prefix} pro Bestellung angewendet werden`,
},
});
return { operations: operations }
}
}
}
}
Would be happy to get help with that.
John