${function() {
const variantData = data.variant || {"id":"11ca7795-8200-4bed-8756-c0536c5ca4fd","product_id":"02425e3d-0925-4c92-a078-b8a4fe0d71d4","title":"","weight_unit":"lb","inventory_quantity":77,"sku":"99746","barcode":"191069997469","position":1,"option1":"","option2":"","option3":"","note":"","image":null,"wholesale_price":[{"price":15.99,"min_quantity":1}],"weight":"0","compare_at_price":"15.99","price":"15.99","retail_price":"15.99","available":true,"url":"\/products\/noni-400mg?variant=11ca7795-8200-4bed-8756-c0536c5ca4fd","available_quantity":77,"options":[],"off_ratio":0,"flashsale_info":[],"sales":100};
const saveType = "amount";
const saveText = "Save {{saved_amount}}";
const productLabelDiscountOn = false;
const sale_price = ` `;
const compare_at_price = ` `;
return `
-
${("Sale price {price}").replace(/\{\s*price\s*\}/, sale_price)}
${("Original price {price}").replace(/\{\s*price\s*\}/, compare_at_price)}
${saveText.replace('\{\{saved_amount\}\}',
saveType == 'percentage'
? variantData.off_ratio + '%'
: ` `
)}
`;
}()}
const TAG = "spz-custom-product-automatic";
class SpzCustomProductAutomatic extends SPZ.BaseElement {
constructor(element) {
super(element);
this.variant_id = '11ca7795-8200-4bed-8756-c0536c5ca4fd';
this.debounceRenderDiscountList = this.win.SPZCore.Types.debounce(this.win,this.renderDiscountList.bind(this), 500);
}
static deferredMount() {
return false;
}
buildCallback() {
this.action_ = SPZServices.actionServiceForDoc(this.element);
this.templates_ = SPZServices.templatesForDoc(this.element);
this.xhr_ = SPZServices.xhrFor(this.win);
this.setupAction_();
this.viewport_ = this.getViewport();
}
mountCallback() {
this.init();
console.log('promotion_complex',{"downgrade":false,"automatic_discount_list":[{"discount_type":"DT_BUY_ONE_GET_ONE","product_enabled":true,"template_type":"text","is_redirection":true,"discount_icon":"\/\/img.staticdj.com\/oss\/operation\/e42568373e54f6f551dcbce82a1c6b12.svg","discount":[{"discount_id":"440768910216670761","discount_type":"DT_BUY_ONE_GET_ONE","discount_method":"DM_AUTOMATIC","config":{"text":"Buy one get one at 50%<\/span> off"},"product_enabled":true,"template_type":"text","is_redirection":true}]}]});
// 监听事件
this.bindEvent_();
}
init() {
this.handleFitTheme();
// 兜底方案, downgrade == true; 需要降级 则要请求接口
this.doRender_({}).then(async () => {
const downgrade = 'false';
if(downgrade != 'false') {
this.renderDiscountList();
}
});
}
async getDiscountList() {
const productId = '02425e3d-0925-4c92-a078-b8a4fe0d71d4';
const variantId = this.variant_id;
const reqBody = {
product_id: productId,
variant_id: variantId,
discount_method: "DM_AUTOMATIC",
customer: {
customer_id: window.C_SETTINGS.customer.customer_id,
email: window.C_SETTINGS.customer.customer_email
}
}
const url = `/api/storefront/promotion/display_setting/text/list`;
const data = await this.xhr_.fetchJson(url, {
method: "post",
body: reqBody
}).then(res => {
return res;
}).catch(err => {
this.setContainerDisabled(false);
})
return data;
}
async renderDiscountList() {
this.setContainerDisabled(true);
const data = await this.getDiscountList();
this.setContainerDisabled(false);
// 重新渲染 抖动问题处理
const parentDiv = document.querySelector('.automatic_discount_container');
const oldDom = parentDiv.querySelector('.automatic_discount_wrapper');
const newTplDom = await this.getRenderTemplate(data);
if(oldDom){
parentDiv.replaceChild(newTplDom, oldDom);
} else {
parentDiv.appendChild(newTplDom);
}
}
doRender_(data) {
const renderData = data || {};
return this.templates_
.findAndRenderTemplate(this.element, renderData)
.then((el) => {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
this.element.appendChild(el);
});
}
async getRenderTemplate(data) {
const renderData = data || {};
return this.templates_
.findAndRenderTemplate(this.element, renderData)
.then((el) => {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
return el;
});
}
setContainerDisabled(isDisable) {
const automaticDiscountEl = document.querySelector('.automatic_discount_container');
if(isDisable) {
automaticDiscountEl.setAttribute('disabled', '');
} else {
automaticDiscountEl.removeAttribute('disabled');
}
}
// 绑定事件
bindEvent_() {
window.addEventListener('click', (e) => {
let containerNodes = document.querySelectorAll(".automatic-container .panel");
let bool;
Array.from(containerNodes).forEach((node) => {
if(node.contains(e.target)){
bool = true;
}
})
// 是否popover面板点击范围
if (bool) {
return;
}
if(e.target.classList.contains('drowdown-icon') || e.target.parentNode.classList.contains('drowdown-icon')){
return;
}
const nodes = document.querySelectorAll('.automatic-container');
Array.from(nodes).forEach((node) => {
node.classList.remove('open-dropdown');
})
// 兼容主题
this.toggleProductSticky(true);
})
// 监听变体变化
document.addEventListener('dj.variantChange', async(event) => {
// 重新渲染
const variant = event.detail.selected;
if (variant.product_id == '02425e3d-0925-4c92-a078-b8a4fe0d71d4') {
this.variant_id = variant.id;
}
await this.debounceRenderDiscountList();
});
}
// 兼容主题
handleFitTheme() {
// top 属性影响抖动
let productInfoEl = null;
if (window.SHOPLAZZA.theme.merchant_theme_name === 'Wind' || window.SHOPLAZZA.theme.merchant_theme_name === 'Flash') {
productInfoEl = document.querySelector('.product-info-body .product-sticky-container');
} else if (window.SHOPLAZZA.theme.merchant_theme_name === 'Hero') {
productInfoEl = document.querySelector('.product__info-wrapper .properties-content');
}
if(productInfoEl){
productInfoEl.classList.add('force-top-auto');
}
}
// 兼容 wind/flash /hero 主题 (sticky属性影响 popover 层级展示, 会被其他元素覆盖)
toggleProductSticky(isSticky) {
let productInfoEl = null;
if (window.SHOPLAZZA.theme.merchant_theme_name === 'Wind' || window.SHOPLAZZA.theme.merchant_theme_name === 'Flash') {
productInfoEl = document.querySelector('.product-info-body .product-sticky-container');
} else if (window.SHOPLAZZA.theme.merchant_theme_name === 'Hero') {
productInfoEl = document.querySelector('.product__info-wrapper .properties-content');
}
if(productInfoEl){
if(isSticky) {
// 还原该主题原有的sticky属性值
productInfoEl.classList.remove('force-position-static');
return;
}
productInfoEl.classList.toggle('force-position-static');
}
}
setupAction_() {
this.registerAction('handleDropdown', (invocation) => {
const discount_id = invocation.args.discount_id;
const nodes = document.querySelectorAll('.automatic-container');
Array.from(nodes).forEach((node) => {
if(node.getAttribute('id') != `automatic-${discount_id}`) {
node.classList.remove('open-dropdown');
}
})
const $discount_item = document.querySelector(`#automatic-${discount_id}`);
$discount_item && $discount_item.classList.toggle('open-dropdown');
// 兼容主题
this.toggleProductSticky();
});
}
triggerEvent_(name, data) {
const event = SPZUtils.Event.create(this.win, `${ TAG }.${ name }`, data || {});
this.action_.trigger(this.element, name, event);
}
isLayoutSupported(layout) {
return layout == SPZCore.Layout.CONTAINER;
}
}
SPZ.defineElement(TAG, SpzCustomProductAutomatic);
Buy one get one at 50% off
Buy one get one at 50% off
Buy one get one at 50% off
${(function () {
const automatic_discount_list = originData.automatic_discount_list;
if(automatic_discount_list?.length > 0) {
return `
${(function () {
return automatic_discount_list.map((item) => {
const template_type = item.discount[0].template_type;
const is_show_automatic_discount = item.discount[0].product_enabled;
const is_redirection = item.discount[0].is_redirection;
const discount_icon = item.discount_icon;
const first_automatic_discount = item.discount[0];
const first_landing_url = `/promotions/discount-default/${first_automatic_discount.discount_id}`;
const automatic_discount_total = item.discount.length;
const isHasDiscountIcon = discount_icon ? true : false;
const isHasDiscountIconWithNoTag = (template_type != 'tag' && isHasDiscountIcon)? true : false;
return `
${first_automatic_discount.config.text}
${first_automatic_discount.config.text}
${function() {
return item.discount.map(childItem => {
return `
`}).join('');
}()}
`;
}).join('');
})()}
`
} else {
return `
`;
}
})()}
${function(){
const variantData = data.variant || {"id":"11ca7795-8200-4bed-8756-c0536c5ca4fd","product_id":"02425e3d-0925-4c92-a078-b8a4fe0d71d4","title":"","weight_unit":"lb","inventory_quantity":77,"sku":"99746","barcode":"191069997469","position":1,"option1":"","option2":"","option3":"","note":"","image":null,"wholesale_price":[{"price":15.99,"min_quantity":1}],"weight":"0","compare_at_price":"15.99","price":"15.99","retail_price":"15.99","available":true,"url":"\/products\/noni-400mg?variant=11ca7795-8200-4bed-8756-c0536c5ca4fd","available_quantity":77,"options":[],"off_ratio":0,"flashsale_info":[],"sales":100};
return `
Sku:
${variantData && variantData.sku}
`
}()}
Add to cart
$15.99
${function(){
const wholesale_enabled = false;
const qty = data.quantity || 1;
const currentSelectVariant = data.variant;
const defaultVariant = (data.product && data.product.variants && data.product.variants[0]);
const productVariant = {"id":"11ca7795-8200-4bed-8756-c0536c5ca4fd","product_id":"02425e3d-0925-4c92-a078-b8a4fe0d71d4","title":"","weight_unit":"lb","inventory_quantity":77,"sku":"99746","barcode":"191069997469","position":1,"option1":"","option2":"","option3":"","note":"","image":null,"wholesale_price":[{"price":15.99,"min_quantity":1}],"weight":"0","compare_at_price":"15.99","price":"15.99","retail_price":"15.99","available":true,"url":"\/products\/noni-400mg?variant=11ca7795-8200-4bed-8756-c0536c5ca4fd","available_quantity":77,"options":[],"off_ratio":0,"flashsale_info":[],"sales":100};
const variantData = currentSelectVariant || defaultVariant || productVariant;
const wholesale_price = variantData.wholesale_price || [];
if(wholesale_enabled && wholesale_price.length > 0) {
let wholesaleIndex = wholesale_price.findIndex(item => {
return item.min_quantity > qty;
});
if(wholesaleIndex < 0){
wholesaleIndex = wholesale_price.length - 1;
}else if(wholesaleIndex > 0){
wholesaleIndex = wholesaleIndex - 1;
}
const wholesalePrice = wholesale_price[wholesaleIndex] || '';
return `
`
}else {
const price = variantData && variantData.price;
return price != undefined ? `
` : ' ';
}
}()}
Buy now
Product was out of stock.
Product is unavailable.