/* eslint-disable indent */
/* eslint-disable arrow-body-style */
/* eslint-disable eqeqeq */
/* eslint-disable consistent-return */
/* eslint-disable no-use-before-define */
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
/* eslint-disable camelcase */
const v2_in_cart_drawer_crossell_block_html = `
{{-- Insert modal content here --}}
`
const v2_in_cart_drawer_qty_block_html = `
{{-- Insert modal content here --}}
`;
const v2_in_cart_drawer_sticky =
'';
const v2_in_cart_drawer_carousel = `
`;
let v2_cart_drawer_product_id = $('input[name=\'product_id\']').val()
let v2_cart_drawer_offer_id = null
let v2_cart_drawer_cart_id = ''
let v2_cart_drawer_cart_items = []
let v2_cart_drawer_hasOffer = false
const v2_cart_drawer_position = 6
const v2_cart_drawer_offer_showed = false
let v2_cart_drawer_req_data = {}
let v2_cart_drawer_loading = true
let cartDrawerOfferSetting = null
let v2_check_existed_cart_drawer = false
let listOffers = []
function getPopupContentCartLocation() {
$.get('/api/storefront/carts', (cart_data) => {
if (cart_data && cart_data.length > 0) {
v2_cart_drawer_req_data.is_sticky =
cartDrawerOfferSetting.upssell_setting_data.isSticky ==
'true'
? 1
: 0;
v2_cart_drawer_cart_id = cart_data[0].id;
v2_cart_drawer_cart_items = cart_data[0].lineItems.physicalItems.concat(
cart_data[0].lineItems.digitalItems,
);
v2_cart_drawer_req_data.products_in_cart = v2_cart_drawer_cart_items;
v2_cart_drawer_req_data.trigger_product_ids = v2_cart_drawer_cart_items.map(
(x) => x.productId,
);
if (localStorage.getItem('v2_cart_drawer_offer_showed')) {
return false;
}
localStorage.removeItem('offer_showed');
v2_cart_drawer_req_data.trigger_variant_ids = v2_cart_drawer_cart_items.map(
(x) => x.variantId,
);
}
v2_dest_currency_code = $('.body').attr('data-currency-code');
if (
typeof v2_dest_currency_code !== 'undefined' &&
v2_dest_currency_code !== v2_base_currency_code
) {
localStorage.setItem(
'v2_dest_currency_code',
v2_dest_currency_code,
);
} else if (
typeof v2_dest_currency_code !== 'undefined' &&
v2_dest_currency_code === v2_base_currency_code
) {
v2_dest_currency_code = v2_base_currency_code;
localStorage.setItem(
'v2_dest_currency_code',
v2_dest_currency_code,
);
} else if (
localStorage.getItem('v2_dest_currency_code') &&
localStorage.getItem('v2_dest_currency_code') !==
v2_base_currency_code
) {
v2_dest_currency_code = localStorage.getItem(
'v2_dest_currency_code',
);
} else {
v2_dest_currency_code = v2_base_currency_code;
}
v2_cart_drawer_req_data.currency_code = v2_dest_currency_code;
if (
v2_cart_drawer_req_data.trigger_product_ids.length < 1 &&
v2_cart_drawer_product_id == null
) {
console.log('Request data invalid. Request fail.');
v2_cart_drawer_loading = false;
v2_cart_drawer_hasOffer = false;
return false;
}
if (
cartDrawerOfferSetting.upssell_setting_data.numberOfOffer !==
'All'
) {
v2_cart_drawer_req_data.numberOfOffer =
cartDrawerOfferSetting.upssell_setting_data.numberOfOffer;
}
let checkTimes = 0;
checkExistedCartDrawer = setInterval(() => {
if (v2_check_existed_cart_drawer || checkTimes == 3) {
clearInterval(checkExistedCartDrawer);
}
if (
$('#v3CartDrawer').length > 0 &&
!(v2_check_existed_cart_drawer || checkTimes == 3)
) {
$.ajax({
url: `${v2_apiUri}/api/popup-content-cart-drawer`,
headers: {
'Access-Control-Allow-Headers': '*',
},
type: 'GET',
crossDomain: true,
data: v2_cart_drawer_req_data,
dataType: 'json',
success(res) {
v2_cart_drawer_loading = false;
if (res.listOffers) {
listOffers = res.listOffers;
if (
cartDrawerOfferSetting.upssell_setting_data
.isSticky == 'true'
) {
if($("#v2_in_cart_drawer_sticky").length == 0) {
$(
'#v3CartDrawer .cart-drawer__footer',
).prepend(v2_in_cart_drawer_sticky);
}
res.listOffers = res.listOffers.filter(
(e) =>
e.templateName ==
'cart-drawer-crosssell',
);
if (res.listOffers.length > 1) {
$('#v2_in_cart_drawer_sticky').html(
v2_in_cart_drawer_carousel,
);
res.listOffers.forEach(
(offerRes, index) => {
if (index !== 0) {
$(
'#carouselCartDrawerOffer .carousel-indicators',
).append(
``,
);
}
v2_subStrings = offerRes.templateName.split(
'-',
);
$(
'#v2_in_cart_drawer_sticky .carousel-inner',
).append(`
${renderContentBlock(
offerRes.responseText,
res.templateName,
)}
`);
v2_cart_drawer_hasOffer = true;
console.log('(v2) Found offer');
v2_cart_drawer_offer_id =
res.offer_id;
v2_cart_drawer_product_id =
res.trigger_product_id;
v2ShowOfferBlock(
v2_subStrings[1],
v2_subStrings[2],
);
},
);
} else {
const v2_subStrings = res.listOffers[0].templateName.split(
'-',
);
if (
res.listOffers[0].templateName ===
'cart-drawer-crosssell'
) {
$('#v2_in_cart_drawer_sticky').html(
v2_in_cart_drawer_crossell_block_html,
);
$(
'#in-cart-drawer-crossell-block',
).html(
res.listOffers[0].responseText,
);
v2_cart_drawer_hasOffer = true;
console.log('(v2) Found offer');
v2_cart_drawer_offer_id =
res.listOffers[0].offer_id;
v2_cart_drawer_product_id =
res.listOffers[0]
.trigger_product_id;
v2ShowOfferBlock(
v2_subStrings[1],
v2_subStrings[2],
);
}
}
} else {
const triggerProductId =
res.listOffers[0].trigger_product_id;
if (res.listOffers.length > 1) {
res.listOffers = res.listOffers.filter(
(e) =>
!(
e.templateName ==
'cart-drawer-discount_quantity' &&
e.content
.offerInCartDrawerQtyDiscount
.content.length == 0
),
);
v2_in_cart_drawer_carousel_block = `${v2_in_cart_drawer_carousel}
`;
$(
`#v3CartDrawer #cartDrawerList .productId-${triggerProductId}`,
)
.first()
.append(
v2_in_cart_drawer_carousel_block,
);
renderSliderOffers(res.listOffers);
} else {
const v2_subStrings = res.listOffers[0].templateName.split(
'-',
);
if (
res.listOffers[0].templateName ===
'cart-drawer-crosssell'
) {
$(
`#v3CartDrawer #cartDrawerList .productId-${triggerProductId}`,
)
.first()
.append(
v2_in_cart_drawer_crossell_block_html,
);
$(
'#in-cart-drawer-crossell-block',
).html(
res.listOffers[0].responseText,
);
} else if (
res.listOffers[0].templateName ===
'cart-drawer-discount_quantity'
) {
$(
`#v3CartDrawer #cartDrawerList .productId-${triggerProductId}`,
)
.first()
.append(
v2_in_cart_drawer_qty_block_html,
);
$('#in-cart-drawer-qty-block').html(
res.listOffers[0].responseText,
);
}
v2_cart_drawer_hasOffer = true;
console.log('(v2) Found offer');
v2_cart_drawer_offer_id =
res.listOffers[0].offer_id;
v2_cart_drawer_product_id =
res.listOffers[0]
.trigger_product_id;
v2ShowOfferBlock(
v2_subStrings[1],
v2_subStrings[2],
);
}
}
}
console.log(
'(v2) Load completed. Request time: ',
new Date().getTime(),
);
},
error(res) {
v2_cart_drawer_loading = false;
console.log(
'(v2) Error occured. Request time: ',
new Date().getTime(),
);
},
});
v2_check_existed_cart_drawer = true;
} else {
checkTimes++;
}
}, 1000);
});
}
function getCartDrawerOfferSetting() {
$.get(
`${v2_cart_native_app_url}/api/cartdrawer-upsell-crosssell?store_hash=${v2_store_hash}`,
(data) => {
cartDrawerOfferSetting = {
is_installed: data.data.is_installed,
upssell_setting_data: JSON.parse(
data.data.upssell_setting_data,
),
};
if (
cartDrawerOfferSetting &&
cartDrawerOfferSetting.is_installed &&
cartDrawerOfferSetting.upssell_setting_data.numberOfOffer
) {
getPopupContentCartLocation();
}
},
);
}
function renderContentBlock(content, offerType = 'cart-drawer-crosssell') {
if (offerType === 'cart-drawer-crosssell') {
return `
${content}
`;
}
return `
${content}
`;
}
function renderSliderOffers(listOffers) {
listOffers.forEach((offerRes, index) => {
if (index != 0) {
$('#carouselCartDrawerOffer .carousel-indicators').append(
``,
);
}
v2_subStrings = offerRes.templateName.split('-');
$('#v3CartDrawer .carousel-inner').append(
`
${renderContentBlock(
offerRes.responseText,
offerRes.templateName,
)}
`,
);
console.log('(v2) Found offer');
v2ShowOfferBlock(v2_subStrings[1], v2_subStrings[2]);
});
}
$(() => {
const v2_start_time = new Date().getTime()
linkCssCartDrawerCrossell = `${v2_apiUri}/v2/storefront/css/cart-drawer-crosssell.css?v=${new Date().getTime()}`
linkCssCartDrawerQty = `${v2_apiUri}/v2/storefront/css/cart-drawer-qty_discount.css?v=${new Date().getTime()}`
$('head').append(``)
$('head').append(``)
v2_cart_drawer_req_data = {
store_hash: v2_store_hash,
trigger_product_ids: [],
products_in_cart: [],
currency_code: '',
screen_width: $(window).width(),
is_user_logged_in: 0,
user_token: '',
trigger_variant_ids: [],
cart_items: [],
is_sticky: 0
}
$('.navUser-item--cart, #form-action-addToCart').on(
'click touchstart',
() => {
$.getScript(`${v2_apiUri}/storefront/js/popup-widget.js`, () => {
$.get({
url: `/customer/current.jwt?app_client_id=${v2_app_client_id}`,
success(response) {
v2_cart_drawer_req_data.is_user_logged_in = 1;
v2_cart_drawer_req_data.user_token = response;
getCartDrawerOfferSetting();
},
error(error) {
console.log('failed to get current user');
console.log(error);
getCartDrawerOfferSetting();
},
});
});
},
);
})