Skip to content

Commit

Permalink
fix(template/fetch-info): properly fetch apply discount when coupon f…
Browse files Browse the repository at this point in the history
…rom url param is persisted
  • Loading branch information
leomp12 committed Sep 28, 2024
1 parent 9c328c2 commit 48cff23
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 4 additions & 6 deletions @ecomplus/storefront-template/template/js/lib/fetch-info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { modules } from '@ecomplus/client'
import { price as getPrice } from '@ecomplus/utils'
import emitter from './emitter'
import utm from './persist-utm'
import { utm, sessionCoupon } from './persist-utm'

window._info = window._info || {}
const fetchInfoPromises = []
Expand All @@ -11,7 +11,7 @@ const modulesToFetch = Array.isArray(window.modulesToFetch)
{ endpoint: 'list_payments' },
{ endpoint: 'calculate_shipping' }
]
if (Object.keys(utm).length) {
if (Object.keys(utm).length || sessionCoupon) {
const {
resource,
body: contextBody
Expand All @@ -27,10 +27,8 @@ if (Object.keys(utm).length) {
}]
}
}
const urlParams = new URLSearchParams(window.location.search)
const discountCoupon = urlParams.get('coupon')
if (discountCoupon) {
applyDiscountData.discount_coupon = discountCoupon
if (sessionCoupon) {
applyDiscountData.discount_coupon = sessionCoupon
}
modulesToFetch.push({
endpoint: 'apply_discount',
Expand Down
7 changes: 5 additions & 2 deletions @ecomplus/storefront-template/template/js/lib/persist-utm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ if (isCurrentUtm) {
if (urlParams.get('referral') && !sessionStorage.getItem('ecomReferral')) {
sessionStorage.setItem('ecomReferral', urlParams.get('referral'))
}
if (urlParams.get('coupon') && !sessionStorage.getItem('st_discount_coupon')) {
sessionStorage.setItem('st_discount_coupon', urlParams.get('coupon'))
const sessionCoupon = urlParams.get('coupon') || sessionStorage.getItem('st_discount_coupon')
if (sessionCoupon && !sessionStorage.getItem('st_discount_coupon')) {
sessionStorage.setItem('st_discount_coupon', sessionCoupon)
}

export default utm

export { utm, sessionCoupon }

0 comments on commit 48cff23

Please sign in to comment.