${function() {
const variantData = data.variant || {"id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"8bd5208f-7d65-413e-b30d-e406dc83d38f","title":"Black Tortoiseshell","weight_unit":"kg","inventory_quantity":8,"sku":"ZSA3003-V2","barcode":"","position":1,"option1":"Black Tortoiseshell","option2":"","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/f1c91b159082eb27f736f7b97280a70e.jpg","path":"f1c91b159082eb27f736f7b97280a70e.jpg","width":5000,"height":5000,"alt":"Muse Zealous","aspect_ratio":1},"wholesale_price":[{"price":58,"min_quantity":1}],"weight":"0","compare_at_price":"73","price":"58.00","retail_price":"73","available":true,"url":"\/products\/muse?variant=1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","available_quantity":8,"options":[{"name":"Frame Color","value":"Black Tortoiseshell"}],"off_ratio":"21","flashsale_info":{"variant_id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"","quantity":0,"discount_id":"c8a27b12-5a0a-4cd6-a99b-c9c171938212","limit_time":1567995,"limit_buy":-1,"user_limit_buy":-1,"discount_sales":0,"discount_sales_rate":"0","discount_stock":8,"ends_at":1751860799,"starts_at":1750172822,"allow_oversold":"uncheck","allocation_method":"none","price":"58.00","compare_at_price":"73.00","discount_price":"58.00","customary_saved_price":"15.00","customary_off_ratio":"21","discount_saved_price":"15.00","discount_off_ratio":"21","use_before_price":false,"before_price":"0","title":"","properties":"","color_setting_promotional_copy":"Canada Day Sale","discount_quantity":0,"is_need_split":false},"sales":0};
const saveType = "percentage";
const saveText = "Save {{saved_amount}}";
const productLabelDiscountOn = true;
return `
-
${saveText.replace('\{\{saved_amount\}\}',
saveType == 'percentage'
? variantData.off_ratio + '%'
: ` `
)}
`;
}()}
${function(){
const variantData = data.variant || {"id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"8bd5208f-7d65-413e-b30d-e406dc83d38f","title":"Black Tortoiseshell","weight_unit":"kg","inventory_quantity":8,"sku":"ZSA3003-V2","barcode":"","position":1,"option1":"Black Tortoiseshell","option2":"","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/f1c91b159082eb27f736f7b97280a70e.jpg","path":"f1c91b159082eb27f736f7b97280a70e.jpg","width":5000,"height":5000,"alt":"Muse Zealous","aspect_ratio":1},"wholesale_price":[{"price":58,"min_quantity":1}],"weight":"0","compare_at_price":"73","price":"58.00","retail_price":"73","available":true,"url":"\/products\/muse?variant=1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","available_quantity":8,"options":[{"name":"Frame Color","value":"Black Tortoiseshell"}],"off_ratio":"21","flashsale_info":{"variant_id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"","quantity":0,"discount_id":"c8a27b12-5a0a-4cd6-a99b-c9c171938212","limit_time":1567995,"limit_buy":-1,"user_limit_buy":-1,"discount_sales":0,"discount_sales_rate":"0","discount_stock":8,"ends_at":1751860799,"starts_at":1750172822,"allow_oversold":"uncheck","allocation_method":"none","price":"58.00","compare_at_price":"73.00","discount_price":"58.00","customary_saved_price":"15.00","customary_off_ratio":"21","discount_saved_price":"15.00","discount_off_ratio":"21","use_before_price":false,"before_price":"0","title":"","properties":"","color_setting_promotional_copy":"Canada Day Sale","discount_quantity":0,"is_need_split":false},"sales":0};
return `
Sku : ${variantData && variantData.sku}
Weight : ${variantData && variantData.weight}${variantData && variantData.weight_unit}
Barcode : ${variantData && variantData.barcode}
`
}()}
${function(){
const isRTL = originData.isRTL;
const isMobile = originData.isMobile;
const inProductDetail = originData.inProductDetail;
if (!inProductDetail) {
return `
Flash sale cannot be displayed here. Please move it to the product detail area.
(This prompt will not be displayed on the client-side)
`;
}
const flashsaleData = originData.flashsaleData;
const image_domain = originData.image_domain;
const discount_info = flashsaleData.discount_info;
const setting = flashsaleData.product_setting;
if (!discount_info || !setting) return ``;
const displayType = setting.display_type;
const banner = setting.banner;
const productDisplay = setting.product_display;
const bannerConfig = JSON.parse(banner.config);
const productDisplayConfig = JSON.parse(productDisplay.config);
const colorConfig = productDisplayConfig.color;
const countdownConfig = productDisplayConfig.countdown;
let titleIcon = ``;
let bgImg = ``;
const deg = `${isRTL ? -90 : 90}deg`;
const { banner_bg_start, banner_bg_end, banner_text } = colorConfig;
let bgStyle = `background: linear-gradient(${deg}, ${banner_bg_start} 0%, ${banner_bg_end} 100%);`;
let bannerColor = `color: ${banner_text};`
if (banner.type === "TYPE_CUSTOM") {
const { desktop, mobile, image_render } = bannerConfig;
const bgSize = image_render === "fill" ? "background-size: cover;background-position: center;" : "background-position: center;background-size: auto 100%;";
if (isMobile && mobile) {
bgStyle = `background: URL(${image_domain + mobile});${bgSize}`;
} else if (!isMobile && desktop) {
bgStyle = `background: URL(${image_domain + desktop});${bgSize}`;
}
} else {
if (banner.type === "TYPE_ONE") {
titleIcon = productDisplay.text ? `
` : "";
bgImg = ``
}
if (banner.type === "TYPE_TWO") {
titleIcon = productDisplay.text ? `
` : ""
}
}
const { sale_bar_background_color, progress_sale_bar_background_color_start, progress_sale_bar_background_color_end } = colorConfig;
const rate = discount_info.discount_sales_rate;
const progressBarStyle = `background: linear-gradient(${deg}, ${progress_sale_bar_background_color_start} 0%, ${progress_sale_bar_background_color_end} 100%);`;
const saleCount = productDisplay.sales_progress.format === "FORMAT_NUMBER" ? discount_info.discount_sales : `${rate}%`;
const progressBarDom = productDisplay.sales_progress.enabled && rate > 0 ? `
` : ``;
const { countdown_bg, countdown_text } = colorConfig;
const { end_opened, end_format } = countdownConfig;
const tempMap = {
"DD_SSS": "DD:HH:mm:ss:SSS",
"HH_SSS": "HH:mm:ss:SSS",
"DD_ss": "DD:HH:mm:ss",
"HH_ss": "HH:mm:ss"
}
const isShowDD = [tempMap.DD_SSS, tempMap.DD_ss].includes(end_format);
const isShowSSS = [tempMap.DD_SSS, tempMap.HH_SSS].includes(end_format);
const countdown = discount_info.ends_remaining_seconds;
const countdownDom = end_opened ? `
${function() {
if (banner.type === "TYPE_TWO") return `
`
return ``;
}()}
` : ``;
const flashSaleDesc = discount_info.limit_user_product_discount > -1 ? `
Promo products limited to ${discount_info.limit_user_product_discount} item per person
` : "";
return `
${bgImg}
${titleIcon}
${productDisplay.text}
${progressBarDom}
${countdownDom}
${flashSaleDesc}
`}()}
${function(){
const dd = data.dd; const hh = data.hh; const mm = data.mm; const ss = data.ss; const sss = data.SSS;
const hours = data.d * 24 + data.h;
return `
${dd}D
·
${hours}
${hh}
:
${mm}
:
${ss}
.
${sss}
`
}()}
class SpzCustomDiscountFlashsale extends SPZ.BaseElement {
constructor(element) {
super(element);
this.xhr_ = SPZServices.xhrFor(this.win);
this.getFlashSaleApi = "\/api\/storefront\/promotion\/flashsale\/display_setting\/product_setting";
this.timer = null;
this.variantId = "1e8ddd9a-a4f3-47d3-9390-0b221c5fa890";
// 促销活动数据
this.flashsaleData = {}
}
isLayoutSupported(layout) {
return layout == SPZCore.Layout.CONTAINER;
}
buildCallback() {
this.templates_ = SPZServices.templatesForDoc();
this.viewport_ = this.getViewport();
// 挂载bind函数 解决this指向问题
this.render = this.render.bind(this);
this.resize = this.resize.bind(this);
this.switchVariant = this.switchVariant.bind(this);
}
mountCallback() {
// 获取数据
this.getData();
this.element.onclick = (e) => {
const cur = this.win.document.querySelector(".app_discount_flashsale_desc");
if (this.flashsaleData.product_setting.is_redirection && appDiscountUtils.inProductBody(this.element) && e.target !== cur) {
this.win.open(`/promotions/discount-default/${this.flashsaleData.discount_info.id}`);
}
}
// 绑定
this.viewport_.onResize(this.resize);
// 监听子款式切换,重新渲染
this.win.document.addEventListener('dj.variantChange', this.switchVariant);
}
unmountCallback() {
// 解绑
this.viewport_.removeResize(this.resize);
this.win.document.removeEventListener('dj.variantChange', this.switchVariant);
// 清除定时器
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
resize() {
if (this.timer) {
clearTimeout(this.timer)
this.timer = null;
}
this.timer = setTimeout(() => {
this.render();
}, 200)
}
switchVariant(event) {
const variant = event.detail.selected;
if (variant.product_id == '8bd5208f-7d65-413e-b30d-e406dc83d38f' && variant.id != this.variantId) {
this.variantId = variant.id;
this.getData();
}
}
getData() {
const reqBody = {
product_id: "8bd5208f-7d65-413e-b30d-e406dc83d38f",
product_type: "default",
variant_id: this.variantId
}
this.flashsaleData = {};
this.win.fetch(this.getFlashSaleApi, {
method: "POST",
body: JSON.stringify(reqBody),
headers: {
"Content-Type": "application/json"
}
}).then(async (response) => {
if (response.ok) {
this.flashsaleData = await response.json();
this.render();
} else {
this.clearDom();
}
}).catch(err => {
this.clearDom();
});
}
clearDom() {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
}
render() {
this.templates_
.findAndRenderTemplate(this.element, {
isMobile: appDiscountUtils.judgeMobile(),
isRTL: appDiscountUtils.judgeRTL(),
inProductDetail: appDiscountUtils.inProductBody(this.element),
flashsaleData: this.flashsaleData,
image_domain: this.win.SHOPLAZZA.image_domain,
})
.then((el) => {
this.clearDom();
this.element.appendChild(el);
})
}
}
SPZ.defineElement('spz-custom-discount-flashsale', SpzCustomDiscountFlashsale);
Frame Color
: Black Tortoiseshell
${function(){
const optName = "Frame Color";
const optionValue = data.originData.selectData ? data.originData.selectData[optName].value : data.originData.value;
const optionValueText = optionValue ? (': ' + optionValue) : '';
return `
${optionValueText}
`
}()}
${function(){
const tipText = "Please select a {{ name }}".replace(/\{\{\s+name\s+\}\}/g, data);
return `${tipText}
`
}()}
Free Premium Packaging (Including Outer Box, Sunglasses Case And Microfiber Cloth)
${data.button_text}
${
function(){
var available = true;
return available ? `
${ data.title ? `
` : '' }
` : `
Sold out `
}()
}
Add to cart
$58.00
${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":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"8bd5208f-7d65-413e-b30d-e406dc83d38f","title":"Black Tortoiseshell","weight_unit":"kg","inventory_quantity":8,"sku":"ZSA3003-V2","barcode":"","position":1,"option1":"Black Tortoiseshell","option2":"","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/f1c91b159082eb27f736f7b97280a70e.jpg","path":"f1c91b159082eb27f736f7b97280a70e.jpg","width":5000,"height":5000,"alt":"Muse Zealous","aspect_ratio":1},"wholesale_price":[{"price":58,"min_quantity":1}],"weight":"0","compare_at_price":"73","price":"58.00","retail_price":"73","available":true,"url":"\/products\/muse?variant=1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","available_quantity":8,"options":[{"name":"Frame Color","value":"Black Tortoiseshell"}],"off_ratio":"21","flashsale_info":{"variant_id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"","quantity":0,"discount_id":"c8a27b12-5a0a-4cd6-a99b-c9c171938212","limit_time":1567995,"limit_buy":-1,"user_limit_buy":-1,"discount_sales":0,"discount_sales_rate":"0","discount_stock":8,"ends_at":1751860799,"starts_at":1750172822,"allow_oversold":"uncheck","allocation_method":"none","price":"58.00","compare_at_price":"73.00","discount_price":"58.00","customary_saved_price":"15.00","customary_off_ratio":"21","discount_saved_price":"15.00","discount_off_ratio":"21","use_before_price":false,"before_price":"0","title":"","properties":"","color_setting_promotional_copy":"Canada Day Sale","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 ? `
` : ' ';
}
}()}
Product was out of stock.
Product is unavailable.
(function(){
const TAG = 'spz-custom-lamb-add-btn';
class SpzCustomLambAddBtn extends SPZ.BaseElement {
constructor(element) {
super(element);
this.templates_ = null;
this.action_ = null;
this.selected_variant = '{"id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"8bd5208f-7d65-413e-b30d-e406dc83d38f","title":"Black Tortoiseshell","weight_unit":"kg","inventory_quantity":8,"sku":"ZSA3003-V2","barcode":"","position":1,"option1":"Black Tortoiseshell","option2":"","option3":"","note":"","image":{"src":"\/\/img.staticdj.com\/f1c91b159082eb27f736f7b97280a70e.jpg","path":"f1c91b159082eb27f736f7b97280a70e.jpg","width":5000,"height":5000,"alt":"Muse Zealous","aspect_ratio":1},"wholesale_price":[{"price":58,"min_quantity":1}],"weight":"0","compare_at_price":"73","price":"58.00","retail_price":"73","available":true,"url":"\/products\/muse?variant=1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","available_quantity":8,"options":[{"name":"Frame Color","value":"Black Tortoiseshell"}],"off_ratio":"21","flashsale_info":{"variant_id":"1e8ddd9a-a4f3-47d3-9390-0b221c5fa890","product_id":"","quantity":0,"discount_id":"c8a27b12-5a0a-4cd6-a99b-c9c171938212","limit_time":1567995,"limit_buy":-1,"user_limit_buy":-1,"discount_sales":0,"discount_sales_rate":"0","discount_stock":8,"ends_at":1751860799,"starts_at":1750172822,"allow_oversold":"uncheck","allocation_method":"none","price":"58.00","compare_at_price":"73.00","discount_price":"58.00","customary_saved_price":"15.00","customary_off_ratio":"21","discount_saved_price":"15.00","discount_off_ratio":"21","use_before_price":false,"before_price":"0","title":"","properties":"","color_setting_promotional_copy":"Canada Day Sale","discount_quantity":0,"is_need_split":false},"sales":0}';
this.lens_process_id = "";
}
buildCallback() {
this.xhr_ = SPZServices.xhrFor(this.win);
this.action_ = SPZServices.actionServiceForDoc(this.element);
this.templates_ = SPZServices.templatesForDoc(this.element);
this.getLambLensSteps_(true);
this.setupAction_();
}
mountCallback() {
document.addEventListener('dj.variantChange', (event) => {
const variant = event.detail.selected;
if (variant.product_id == '8bd5208f-7d65-413e-b30d-e406dc83d38f') {
this.selected_variant = JSON.stringify(variant);
}
});
}
fetchLambLensSteps() {
const lens_process_id = location.search.replace('?', '').split('&').find(v => v.includes('lens_process_id'));
const template_id = location.search.replace('?', '').split('&').find(v => v.includes('template_id'));
const stepsUrl = '/api/fireant/product/8bd5208f-7d65-413e-b30d-e406dc83d38f/steps' + ((lens_process_id ? `?${lens_process_id}` : '') || (template_id ? `?${template_id}` : ''));
return fetch(stepsUrl).then((res)=>{
return res.json();
}).then((data)=>{
return data;
})
}
showProductBtn() {
var addToCart = document.getElementById('add_to_cart_normal');
var productQuantityBtn = document.getElementById('product_info_quantity_normal');
addToCart && addToCart.classList.remove('hidden');
productQuantityBtn && productQuantityBtn.classList.remove('hidden');
}
getLambLensSteps_(isInit) {
try{
this.fetchLambLensSteps().then((data)=>{
if(data.errors) {
if(isInit){
this.renderBtn(false);
this.showProductBtn();
}
}else{
if(isInit){
this.lens_process_id = data?.lens_process_id;
let frameOnlyArr = data?.prescription_types?.options.filter((option)=>{
return option.prescription_type === 'Frame Only'
});
let frameOnlyObj = frameOnlyArr.length > 0 ? frameOnlyArr[0] : {};
let available = true;
if(available && (frameOnlyObj?.frame_only_display_at == 'product_detail' || frameOnlyObj?.frame_only_display_at == 'all')){
this.renderBtn({ ...frameOnlyObj, process_type: data.process_type });
}else{
this.renderBtn({ process_type: data.process_type });
}
}
}
});
}catch(e){
this.renderBtn(false);
this.showProductBtn();
}
}
renderBtn = (isRenderProcessBtn) => {
return this.templates_
.findAndRenderTemplate(this.element, isRenderProcessBtn)
.then((el) => {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
this.element.appendChild(el);
});
}
handleClickFrameOnlyBtn_ = async () => {
try {
const reqBody = {
product_id: "8bd5208f-7d65-413e-b30d-e406dc83d38f",
variant_id: JSON.parse(this.selected_variant)?.id,
quantity: 1,
properties: {
lens_processing_id: this.lens_process_id,
prescription_type: "Frame Only"
}
}
const data = await this.xhr_.fetchJson('/api/fireant/v2/customize_cart', {
method: "post",
body: reqBody
});
if(data.state === 'success') {
this.atc_loading_ = false;
this.element.removeAttribute('atc-loading');
this.triggerEvent_("dj.addToCart", data);
window.location.href = "/cart";
} else {
this.atc_loading_ = false;
this.element.removeAttribute('atc-loading');
}
} catch (e) {
this.atc_loading_ = false;
this.element.removeAttribute('atc-loading');
const toast = SPZCore.Dom.scopedQuerySelector(document, `#error-toast`);
toast && SPZ.whenApiDefined(toast).then((api) => {
e.then((result)=>{
api.showToast(result?.errors[0] || 'Unknown error');
})
});
}
}
trackAddToCart = () => {
const params = {
business_type: "product_plugin",
function_name: "prescription_lens",
plugin_name: "prescription_lens",
module: "apps",
tab_name: "process_btn",
event_type: "click",
event_developer: "jozy",
event_info: JSON.stringify({
action_type: "frame_only_add_to_cart",
product_id: '"8bd5208f-7d65-413e-b30d-e406dc83d38f"',
process_id: this.lens_process_id,
process_type: "glasses",
element_type: "button",
element_name: "frame_only_btn"
})
}
window.spzutm && window.spzutm.registerParams('add_to_cart', params );
}
setupAction_ = () => {
this.registerAction('handleClickBtn', (invocation) => {
this.saveAllFormProperties_();
const glassesInfo = {
product:{
title: "Muse",
id: "8bd5208f-7d65-413e-b30d-e406dc83d38f",
image: {"src":"\/\/img.staticdj.com\/f1c91b159082eb27f736f7b97280a70e.jpg","path":"f1c91b159082eb27f736f7b97280a70e.jpg","width":5000,"height":5000,"alt":"Muse Zealous","aspect_ratio":1},
selected_variant: JSON.parse(this.selected_variant)
},
atcType: 'add_lens'
};
const tempElement = document.getElementById('process-request-script');
tempElement && SPZ.whenApiDefined(tempElement).then(async (api) => {
await api.requestLensProcess(glassesInfo, invocation.args.process_type);
});
setTimeout(() => {
const proceeBtn = document.getElementById('lens_add_to_cart');
proceeBtn && proceeBtn.classList.remove('pointer-events-none');
}, 3000);
});
this.registerAction('handleClickFrameOnlyBtn', (invocation) => {
this.trackAddToCart();
this.atc_loading_ = true;
this.element.setAttribute('atc-loading',"");
this.handleClickFrameOnlyBtn_();
});
}
saveAllFormProperties_(){
const formEntries = this.getFormData_();
const properties = this.getFormAllProperties_(formEntries);
// 获取的form所有properties存全局
window.app_lamb_form_properties = properties;
}
getFormData_() {
const form = this.element.closest("form");
const formData = new FormData(form);
const formEntries = Object.fromEntries(formData.entries());
return formEntries;
}
getFormAllProperties_(obj){
const result = {};
Object.entries(obj).forEach(([key, value]) => {
if (key.startsWith("properties")) {
result[key.substring(11, key.length - 1)] = value;
}
});
return result;
}
triggerEvent_(name, params) {
const event = SPZUtils.Event.create(
this.win,
name,
params,
{bubbles: true}
);
this.element.dispatchEvent(event);
}
isLayoutSupported = (layout) => {
return layout == SPZCore.Layout.CONTAINER;
}
}
SPZ.defineElement(TAG, SpzCustomLambAddBtn)
})()