Skip to content

Commit e070d3c

Browse files
leomp12claude
andcommitted
fix(template/fetch-info): apply discount coupons in storefronts only from URL
Changes coupon storage logic to separate URL coupons from checkout coupons. Discount is now only applied to product listings when the coupon comes from URL parameters, while manually applied checkout coupons remain isolated to the checkout flow. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1e10201 commit e070d3c

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

@ecomplus/storefront-template/template/js/lib/fetch-info.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { modules } from '@ecomplus/client'
22
import { price as getPrice } from '@ecomplus/utils'
33
import emitter from './emitter'
4-
import { utm, sessionCoupon } from './persist-utm'
4+
import { utm, urlCoupon } from './persist-utm'
55

66
window._info = window._info || {}
77
const fetchInfoPromises = []
@@ -11,7 +11,7 @@ const modulesToFetch = Array.isArray(window.modulesToFetch)
1111
{ endpoint: 'list_payments' },
1212
{ endpoint: 'calculate_shipping' }
1313
]
14-
if (Object.keys(utm).length || sessionCoupon) {
14+
if (Object.keys(utm).length || urlCoupon) {
1515
const {
1616
resource,
1717
body: contextBody
@@ -27,8 +27,8 @@ if (Object.keys(utm).length || sessionCoupon) {
2727
}]
2828
}
2929
}
30-
if (sessionCoupon) {
31-
applyDiscountData.discount_coupon = sessionCoupon
30+
if (urlCoupon) {
31+
applyDiscountData.discount_coupon = urlCoupon
3232
}
3333
modulesToFetch.push({
3434
endpoint: 'apply_discount',

@ecomplus/storefront-template/template/js/lib/persist-utm.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ if (isCurrentUtm) {
1919
if (urlParams.get('referral') && !sessionStorage.getItem('ecomReferral')) {
2020
sessionStorage.setItem('ecomReferral', urlParams.get('referral'))
2121
}
22-
const sessionCoupon = urlParams.get('coupon') || sessionStorage.getItem('st_discount_coupon')
23-
if (sessionCoupon && !sessionStorage.getItem('st_discount_coupon')) {
24-
sessionStorage.setItem('st_discount_coupon', sessionCoupon)
22+
23+
const couponStorageKey = 'ecomUrlCoupon'
24+
const couponCheckoutStorageKey = 'st_discount_coupon'
25+
const checkoutSessionCoupon = sessionStorage.getItem(couponCheckoutStorageKey)
26+
let urlCoupon = urlParams.get('coupon')
27+
if (urlCoupon) {
28+
sessionStorage.setItem(couponStorageKey, urlCoupon)
29+
} else {
30+
urlCoupon = sessionStorage.getItem(couponStorageKey)
31+
if (checkoutSessionCoupon && urlCoupon && checkoutSessionCoupon !== urlCoupon) {
32+
urlCoupon = null
33+
}
34+
}
35+
const sessionCoupon = urlCoupon || checkoutSessionCoupon
36+
if (sessionCoupon && !checkoutSessionCoupon) {
37+
sessionStorage.setItem(couponCheckoutStorageKey, sessionCoupon)
2538
}
2639

2740
export default utm
2841

29-
export { utm, sessionCoupon }
42+
export { utm, sessionCoupon, urlCoupon }

0 commit comments

Comments
 (0)