š„HOT SALEš„Shaper For Gentlemen
${function() {
const variantData = data.variant || {"id":"85b035fa-ab7e-46bc-af69-1dd5eb227339","product_id":"7bb52e74-670b-4bce-b911-c3e97efd7bc2","title":"Black-M","weight_unit":"kg","inventory_quantity":999,"sku":"","barcode":"","position":1,"option1":"Black","option2":"M","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/b7a584dec10e77a70f29bf342870cbaa.jpeg","path":"b7a584dec10e77a70f29bf342870cbaa.jpeg","width":600,"height":600,"alt":"","aspect_ratio":1},"wholesale_price":[{"price":31.99,"min_quantity":1}],"weight":"0","compare_at_price":"51.99","price":"31.99","retail_price":"51.99","available":true,"url":"\/products\/\ud83d\udd25hot-sale\ud83d\udd25shaper-for-gentlemen?variant=85b035fa-ab7e-46bc-af69-1dd5eb227339","available_quantity":999999999,"options":[{"name":"color","value":"Black"},{"name":"size","value":"M"}],"off_ratio":"38","flashsale_info":{"variant_id":"85b035fa-ab7e-46bc-af69-1dd5eb227339","product_id":"","quantity":0,"discount_id":"","limit_time":0,"limit_buy":0,"user_limit_buy":0,"discount_sales":0,"discount_sales_rate":"","discount_stock":0,"ends_at":0,"starts_at":0,"allow_oversold":"","allocation_method":"","price":"31.99","compare_at_price":"","discount_price":"31.99","customary_saved_price":"","customary_off_ratio":"","discount_saved_price":"","discount_off_ratio":"38","use_before_price":false,"before_price":"","title":"","properties":"","color_setting_promotional_copy":"","discount_quantity":0,"is_need_split":false},"sales":0};
const saveType = "amount";
const productLabelDiscountOn = false;
return `
-
${saveType == 'percentage'
? `-${variantData.off_ratio}% `
: `- `
}
`;
}()}
color:
Black
${function(){
return `${data.value} `;
}()}
${function(){
const tipText = "Please select a {{ name }}".replace(/\{\{\s+name\s+\}\}/g, data);
return `${tipText}
`
}()}
size:
M
${function(){
return `${data.value} `;
}()}
${function(){
const tipText = "Please select a {{ name }}".replace(/\{\{\s+name\s+\}\}/g, data);
return `${tipText}
`
}()}
Add to cart
$31.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":"85b035fa-ab7e-46bc-af69-1dd5eb227339","product_id":"7bb52e74-670b-4bce-b911-c3e97efd7bc2","title":"Black-M","weight_unit":"kg","inventory_quantity":999,"sku":"","barcode":"","position":1,"option1":"Black","option2":"M","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/b7a584dec10e77a70f29bf342870cbaa.jpeg","path":"b7a584dec10e77a70f29bf342870cbaa.jpeg","width":600,"height":600,"alt":"","aspect_ratio":1},"wholesale_price":[{"price":31.99,"min_quantity":1}],"weight":"0","compare_at_price":"51.99","price":"31.99","retail_price":"51.99","available":true,"url":"\/products\/\ud83d\udd25hot-sale\ud83d\udd25shaper-for-gentlemen?variant=85b035fa-ab7e-46bc-af69-1dd5eb227339","available_quantity":999999999,"options":[{"name":"color","value":"Black"},{"name":"size","value":"M"}],"off_ratio":"38","flashsale_info":{"variant_id":"85b035fa-ab7e-46bc-af69-1dd5eb227339","product_id":"","quantity":0,"discount_id":"","limit_time":0,"limit_buy":0,"user_limit_buy":0,"discount_sales":0,"discount_sales_rate":"","discount_stock":0,"ends_at":0,"starts_at":0,"allow_oversold":"","allocation_method":"","price":"31.99","compare_at_price":"","discount_price":"31.99","customary_saved_price":"","customary_off_ratio":"","discount_saved_price":"","discount_off_ratio":"38","use_before_price":false,"before_price":"","title":"","properties":"","color_setting_promotional_copy":"","discount_quantity":0,"is_need_split":false},"sales":0};
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.
${(function () {
const automatic_discount_list = originData.automatic_discount_list;
// ę¾ē¤ŗē±»å
const DISPLAY_TYPE = {
DTE_FOLD: 'DTE_FOLD', // ęå
DTE_TILE: 'DTE_TILE' // å¹³éŗ
}
const DEFAULT_CONFIG = {
BG: 'rgba(235, 57, 27, 0.04)',
TEXT_COLOR: '#EB391B',
BORDER_COLOR: 'rgb(235, 57, 27)'
};
const isExist = automatic_discount_list?.length > 0 && automatic_discount_list.some(item => item.discount[0].product_enabled);
// å¦ęę²”ęä»»ä½čŖåØęę£ļ¼åéčļ¼é²ę¢gapå ä½
if (!isExist) {
return `
`;
} else {
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 isFold = (item.discount[0].display_type || DISPLAY_TYPE.DTE_FOLD) === DISPLAY_TYPE.DTE_FOLD;
// ęę¬ę°ē»
const text_arr = item.discount[0].config.texts;
// č½å°é”µé¾ę„
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;
// ęÆå¦ę¾ē¤ŗęę£å¾ę äøęØ”ēē±»åäøäøŗtag
const isHasDiscountIconWithNoTag = (template_type != 'tag' && isHasDiscountIcon)? true : false;
// ęę¬é¢č²
let text_color = DEFAULT_CONFIG.TEXT_COLOR;
// čęÆé¢č²
const bgFn = (curBg) => template_type === "text" ? "transparent" : curBg;
let bg_color = bgFn(DEFAULT_CONFIG.BG);
// č¾¹ę”é¢č²
const borderFn = (curBorder) => template_type == "tag" ? curBorder : "initial";
let border_color = borderFn(DEFAULT_CONFIG.BORDER_COLOR);
// 樔ēé
ē½®
let template_config = first_automatic_discount.template_config;
// å
åŗę¹ę”
try {
if(template_config.length !== 0){
template_config = JSON.parse(template_config);
text_color= isHasDiscountIconWithNoTag ? template_config.color[template_type].icon_text_color : template_config.color[template_type].text_color;
bg_color = bgFn(template_config.color[template_type].background_color);
const arrayRgba = bg_color.split(",");
arrayRgba.splice(3, 1, " 1)");
border_color = borderFn(`${arrayRgba.join(",")}`);
}
} catch (error) {
console.error('template_config_error', error);
template_config = {
color: {
[template_type]: {
icon_text_color: DEFAULT_CONFIG.TEXT_COLOR,
text_color: DEFAULT_CONFIG.TEXT_COLOR,
background_color: DEFAULT_CONFIG.BG
}
}
};
}
// ę ē¾
const isTag = template_type == 'tag';
// ęåå横å¹
const isTextAndBanner = template_type == 'text' || template_type == 'banner';
// ęåę ·å¼
const textStyle = `color: ${text_color}; background-color: transparent; border: none;`;
// ę ē¾ę ·å¼
const labelStyle = `color: ${text_color};border: 1px solid ${border_color};background-color:${bg_color};padding: 4px;`;
// 横å¹
ę ·å¼
const bannerStyle = `color: ${text_color};border: none; background-color:${bg_color}`;
// å¤å±ę ·å¼åØę ē¾ę ·å¼äøäøå±ē¤ŗé¢č²é
ē½®ļ¼é¤å¼ę ē¾ē±»åļ¼é¢č²é½åÆä»„åØå¤å±č¦ē
let outerStyle = '';
if (template_type == 'text') {
outerStyle = textStyle;
} else if (template_type == 'tag') {
outerStyle = "border: none;";
} else if (template_type == 'banner') {
outerStyle = bannerStyle;
}
/**
* 1. ę ē¾äøå®ęÆåē¬ę ·å¼å±ē¤ŗē
* 2. ęå ļ¼ęØŖååøå±ļ¼ęåå横å¹
ļ¼åå¹¶ęäøč”ęę¬ļ¼ ę ē¾ļ¼åē¬ę ·å¼å¤ē
* 3. å¹³éŗļ¼ēŗµååøå±ļ¼ęåć横å¹
åę ē¾ļ¼ ę ē¾ļ¼åē¬ę ·å¼å¤ē
*/
let txtHtml = ``;
if (isFold) {
if(isTag) {
// ę ē¾
const spanText = text_arr.map((text) => {
return `
${text} `;
}).join('');
txtHtml = `
${spanText}
`;
} else {
// ęåå横å¹
txtHtml = `
${first_automatic_discount.config.text}
`;
}
} else {
// ęåå横å¹
, ä½ę ē¾ęčŖå·±ēę ·å¼
const spanText = text_arr.map((text) => {
return `
${text} `;
}).join('');
// é½ęÆēŗµååøå±ļ¼ę ē¾ęé“č·, å¤ę“»åØå¤å±ēŗ§ęØŖååøå±
txtHtml = `
${spanText}
`;
}
/**
* ę¾ē¤ŗå¾ę ēå¤ę
*/
const discount_type = item.discount_type;
const isShowRebateIcon = ["DT_REBATE_CTQ_OTP", "DT_REBATE_CTQ_OTR", "DT_REBATE_CTA_OTP", "DT_REBATE_CTA_OTR", "DT_M_N_DISCOUNT"].includes(discount_type) && isTextAndBanner
const isShowBxgyIcon = ["DT_BUY_ONE_GET_ONE", "DT_BUY_X_GET_Y"].includes(discount_type)
const isShowBundleIcon = ["DT_CLASSIC_BUNDLE","DT_MIX_MATCH_BUNDLE"].includes(discount_type);
/**
* ęø²ęäøęę”ęę½å±ēęę£å蔨
*/
const discount_list_html = (curItem) => {
return `
${function() {
return curItem.discount.map(childItem => {
return `
`}).join('');
}()}
`;
}
return `
${discount_list_html(item)}
${function() {
return text_arr.map((text) => {
return `
${text}
`;
}).join('');
}()}
`;
}).join('');
})()}
`
}
})()}
const TAG = "spz-custom-product-automatic";
class SpzCustomProductAutomatic extends SPZ.BaseElement {
constructor(element) {
super(element);
this.variant_id = '85b035fa-ab7e-46bc-af69-1dd5eb227339';
this.isRTL = SPZ.win.document.dir === 'rtl';
}
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();
// ēå¬äŗä»¶
this.bindEvent_();
}
async init() {
this.handleFitTheme();
const data = await this.getDiscountList();
this.renderApiData_(data);
}
async getDiscountList() {
const productId = '7bb52e74-670b-4bce-b911-c3e97efd7bc2';
const variantId = this.variant_id;
const productType = 'default';
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
},
product_type: productType
}
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);
// éę°ęø²ę ęåØé®é¢å¤ē
this.renderApiData_(data);
}
clearDom() {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
}
async renderApiData_(data) {
const parentDiv = document.querySelector('.automatic_discount_container');
const newTplDom = await this.getRenderTemplate(data);
if (parentDiv) {
parentDiv.innerHTML = '';
parentDiv.appendChild(newTplDom);
} else {
console.log('automatic_discount_container is null');
}
}
doRender_(data) {
const renderData = data || {};
return this.templates_
.findAndRenderTemplate(this.element, renderData)
.then((el) => {
this.clearDom();
this.element.appendChild(el);
});
}
async getRenderTemplate(data) {
const renderData = data || {};
return this.templates_
.findAndRenderTemplate(this.element, { ...renderData, isRTL: this.isRTL })
.then((el) => {
this.clearDom();
return el;
});
}
setContainerDisabled(isDisable) {
const automaticDiscountEl = document.querySelector('.automatic_discount_container_outer');
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 == '7bb52e74-670b-4bce-b911-c3e97efd7bc2' && variant.id != this.variant_id) {
this.variant_id = variant.id;
this.renderDiscountList();
}
});
}
// å
¼å®¹äø»é¢
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);