function hooksUpsell() {
window.FastRecommendationWidget.registerHook('upsell-cross-sell-products', ({products, element}) => {
const productElements = [...element.querySelectorAll(".fs-upsell-cross-sell-product")];
for (const productElement of productElements){
const productID = productElement.dataset.id;
const data = products[productID];
let productUrl = data.productURL,
routesRoot = Shopify.routes.root.slice(0, -1);
$.getJSON(routesRoot + productUrl + '.js', function (product) {
if (productElement.querySelectorAll(`.price-box`).length === 0 &&
productElement.querySelector(".info-container")) {
productElement.querySelector(".info-container").insertAdjacentHTML('beforeend', fs_generatePriceBox(product));
}
}.bind(this));
}
});
}
function fs_generatePriceBox(product) {
var html,
currencyStore = Shopify ? Shopify.currency.active : 'GBP',
stock = product.available ? 'InStock' : 'OutOfStock',
additionalClass = product.compare_at_price ? 'price--on-sale' : '';
html = '
';
if (product.compare_at_price && product.compare_at_price > product.price) {
var percent = window.productTranslate.percent_off.replace('%1', (100 - Math.round(product.price / product.compare_at_price * 100)));
html += '' +
' ' + fs_priceFormat(product.price, currencyStore) + '' +
' ' + percent + '' +
'' +
'' +
' ' + fs_priceFormat(product.compare_at_price, currencyStore) + '' +
'' +
'' +
'' +
'';
} else {
html += '' +
' ' + fs_priceFormat(product.price, currencyStore) + '' +
'' +
'' +
'' +
'';
}
html += '
';
return html;
}
function fs_priceFormat(price, currencyStore) {
var price = Intl.NumberFormat(Shopify.locale, {
style: 'currency',
currency: currencyStore
}).format(price / 100);
switch (currencyStore) {
case "AUD":
price = price.replace('A$', 'AU$');
break;
case "CHF":
price = price.replace(/\s+/g, '');
break;
}
return price;
}
// execution here
if (window.FastRecommendationWidget) {
hooksUpsell();
} else {
window.addEventListener('fast-upsell-cross-sell-ready', function () {
hooksUpsell();
});
}