Your shopping bag is empty
Your cart is reserved for
!
class SpzCustomDiscountBundle extends SPZ.BaseElement {
constructor(element) {
super(element);
}
isLayoutSupported(layout) {
return layout == SPZCore.Layout.LOGIC;
}
mountCallback() {
// 同一页可能有多个 block 各自渲染本 snippet,导致逻辑元素 spz-custom-discount-toast(#appsAddCartToastFunc) 重复。
// 仅保留 DOM 中第一个实例,其余重复实例移除自身,保证逻辑元素及其 action 引用(appsAddCartToastFunc)唯一。
const funcs = document.querySelectorAll('#appsAddCartToastFunc');
if (funcs.length > 1 && this.element !== funcs[0]) {
this.element.remove();
return;
}
// 全局折扣 toast 的弹出点分散在多个模板/组件里(含声明式 @atcError),无法逐一在弹出前前置,
// 故挂载时幂等地去重并挂到 body;加购 toast 改为弹出前按需去重(见 showAddToCartToast)
this.ensureSingleToastInBody_('cart_match_discount_toast_wrap');
}
unmountCallback() {}
// 同一页面可能有多个 block 各自渲染本 toast snippet(如商详的 discount_automatic + 购物车抽屉里的折扣横幅),
// 导致 toast 外层出现重复 id。重复 id 下 getElementById/querySelector 及 SPZ 动作引用都只命中第一个,
// 且被关进抽屉的那份会被抽屉的 transform/overflow 裁剪/错位。这里在挂载时幂等去重:
// 只保留一份、移除多余的,并把保留的挂到 body(body 是 position:fixed 最安全的落点,避免被任何祖先裁剪)。可反复调用。
ensureSingleToastInBody_(id) {
const els = Array.from(document.querySelectorAll('#' + id));
if (!els.length) return;
els.slice(1).forEach((el) => el.remove());
const keep = els[0];
if (keep.parentNode !== document.body) {
document.body.appendChild(keep);
}
}
// 主题购物车抽屉是否打开
isInCartDrawer_() {
return !!document.querySelector('[data-section-type="cart_drawer"] spz-sidebar[open]');
}
setupAction_() {
this.registerAction('showAddToCartToast', () => {
// 抽屉内加购:主题本就不会有加购效果,统一弹插件自己的 toast
// 非抽屉:主题有加购代理则沿用主题加购效果,否则插件兜底
const proxyEl = document.getElementById('add-cart-event-proxy');
const inDrawer = this.isInCartDrawer_();
if (!inDrawer && proxyEl) {
return;
}
// 弹出前按需去重并挂到 body(幂等),避免重复 id 命中错对象或被抽屉裁剪/错位
this.ensureSingleToastInBody_('apps_add_cart_toast_wrap');
const toastEl = document.querySelector('#apps-match-drawer-add_to_cart_toast')
SPZ.whenApiDefined(toastEl).then((apis) => {
apis.showToast("Added successfully");
});
});
}
buildCallback() {
this.setupAction_();
};
}
SPZ.defineElement('spz-custom-discount-toast', SpzCustomDiscountBundle);
const TAG = 'spz-custom-discount-cart-banner-info';
const API = {
DISPLAY_TEXT: "\/api\/storefront\/promotion\/cart_page\/display_text",
CART_SELECT: "\/api\/cart\/cart-select?r=",
}
class SpzCustomDiscountCartBannerInfo extends SPZ.BaseElement {
constructor(element) {
super(element);
this.xhr_ = SPZServices.xhrFor(this.win);
this.displayTextData = {}; // 活动数据文案数据
this.selectedCategory = ''; // 当前选中的分类
this.selectedDiscountId = ''; // 当前选中的折扣 id
this.codes = []; // 优惠码数组
}
isLayoutSupported(layout) {
return layout == SPZCore.Layout.LOGIC;
}
mountCallback() {
this.getDisplayText_();
SPZUtils.Event.listen(document, 'dj.cartChange', (e) => {
this.getDisplayText_();
});
//该事件又购物车优惠码组件触发,传参为当前应用的优惠码数组
SPZUtils.Event.listen(document, 'dj.applyDiscountCode', (e) => {
this.codes = e.detail.codes;
this.getDisplayText_();
});
this.registerAction('renderDiscountSelectors', (invocation) => {
const selected = invocation.args?.data;
const groups = this.displayTextData.groups || [];
const discounts = groups.find(v => v.category == selected)?.discounts || []; // 当前分类下的折扣
const defaultDiscountId = discounts[0]?.discount_id; // 默认选中第一个折扣
this.selectedCategory = selected;
this.selectedDiscountId = defaultDiscountId;
this.renderDiscountSelectors_(); // 渲染 Tab
this.renderDiscountText_(); // 渲染活动进度文案
});
this.registerAction('discountSelectorChange', (invocation) => {
const { targetOption } = invocation?.args?.data || {};
this.selectedDiscountId = targetOption;
this.renderDiscountText_(); // 切换活动更新活动进度文案
});
}
getLineItems_(line_items) {
return line_items.map((item) => {
return {
id: item.id,
product_id: item.product_id,
variant_id: item.variant_id,
quantity: item.quantity,
price: item.price,
origin_price: item.origin_price,
compare_at_price: item.compare_at_price,
trunk_price: item.trunk_price,
total_price: item.total_price,
inventory_tracking: item.inventory_tracking,
inventory_policy: item.inventory_policy,
inventory_quantity: item.inventory_quantity,
custom: item.custom,
product_type: item.product_type,
properties: item.properties
};
});
}
getDisplayText_(redo = true) {
this.xhr_.fetchJson(API.CART_SELECT + Math.random().toString(36).slice(-4),
{ method: "GET" }
).then(async (cartData) => {
const cartObj = cartData.cart || {};
if(!cartObj || cartObj.line_items?.length <= 0) {
// 清空购物车时,清空活动数据
this.displayTextData = {};
this.selectedCategory = '';
this.selectedDiscountId = '';
this.renderDiscountBanner_();
return;
}
this.xhr_.fetchJson(API.DISPLAY_TEXT, {
method: "POST",
body: {
line_items: this.getLineItems_(cartObj.line_items),
sales_channel: {
sale_channel_type: "online",
sale_channel_id: window.C_SETTINGS.shop.shop_id
},
codes: this.codes || []
}
}).then(data => {
this.displayTextData = data;
this.renderDiscountBanner_(redo);
// 凑单弹窗加购商品后,重新获取活动进度文案
this.renderDiscountText_(redo);
}).catch(err => {
});
});
}
renderDiscountBanner_(redo = true) {
const bannerEl = document.getElementById("app-discount-cart-banner");
if (bannerEl) {
SPZ.whenApiDefined(bannerEl).then(apis => {
apis.render(this.displayTextData, redo);
});
}
}
renderDiscountSelectors_(redo = true) {
const groups = this.displayTextData.groups || [];
const discounts = groups.find(v => v.category == this.selectedCategory)?.discounts || [];
if (discounts.length <= 0) {
return;
}
const selectorsEl = document.getElementById("app-discount-selectors-render");
if (selectorsEl) {
SPZ.whenApiDefined(selectorsEl).then(apis => {
apis.render(discounts, redo);
});
}
}
renderDiscountText_(redo = true) {
if (!this.selectedCategory || !this.selectedDiscountId) {
return;
}
const groups = this.displayTextData.groups || [];
const discounts = groups.find(v => v.category == this.selectedCategory)?.discounts || [];
const discount = discounts.find(v => v.discount_id == this.selectedDiscountId) || {};
const textEl = document.getElementById("app-discount-text-render");
if (textEl) {
SPZ.whenApiDefined(textEl).then(apis => {
apis.render(discount, redo);
});
}
}
}
SPZ.defineElement(TAG, SpzCustomDiscountCartBannerInfo);