Script 01

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

if (window.matchMedia("(max-width: 768px)").

matches) {
window.onscroll = function() {
var pageOffset = document.documentElement.scrollTop ||
document.body.scrollTop,
btn = document.getElementById('scrollToTop');
if (btn) btn.style.display = pageOffset > 1200 ? 'block' : 'none';
}
}

function updateVariantData() {
var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
var productDataJSON = JSON.parse($("body").find("[data-product-json]").html());
var variantData = productDataJSON.product;

if(urlParams.has('variant')){
var selectedVariantId = urlParams.get('variant');

productDataJSON.product.variants.forEach(function(e) {
if(e.id == selectedVariantId) {
variantData = e;
}
});
}

var mainPrice = variantData.price;


var discountPrice = variantData.compare_at_price || variantData.price;
var installmentsPrice = Math.round((mainPrice * getRate(12)) / 12);
var percentageDiscount = Math.round(((discountPrice - mainPrice) * 100 ) /
discountPrice);
var moneyDiscount = discountPrice - mainPrice;
$(".product-label.product-label--on-sale").html("-" + percentageDiscount +
"%");
$('.installment-page-product').html('ou em <b>12x</b> de <span> R$ ' +
installmentsPrice.toString().slice(0, -2) + "," +
installmentsPrice.toString().slice(-2) + '</span>');

if(variantData.compare_at_price > variantData.price ) {


$(".money-saving").html("Desconto de R$ " + moneyDiscount.toString().slice(0,
-2) + "," + moneyDiscount.toString().slice(-2));
}

if($('#installmentsContainer').length){
var installmentsContainer = $('#installmentsContainer');
var installmentsOptions = '';

for(var installment = 2; installment < 12; installment++){


var formattedPrice = parseFloat(mainPriceReal +'.'+ mainPriceCents);
var installmentPrice = (formattedPrice * getRate(installment)) /
installment;
var notRate = installment < 7 ? ' Sem Juros' : "";
var installmentPriceFormatted =
installmentPrice.toFixed(2).toString().replace(".", ",");

installmentsOptions += '<p class="parcelas">'+ installment +'x de <span


style="color: rgba(0,0,0,.75);"><b>'+ installmentPriceFormatted +'</b></span>'+
notRate +'</p>';
}
installmentsContainer.html(installmentsOptions);
}
}

function getRate(installments){

let rates = {
'7' : 1.1472,
'8' : 1.1623,
'9' : 1.1769,
'10' : 1.1865,
'11' :1.2012,
'12' : 1.2161
};

return rates[installments] ?? 1;
}

$(".block-swatch__radio, .variant-swatch__radio, .product-form__single-


selector").change(function () {
window.setTimeout(function(){
updateVariantData();
}, 100);
});

$(document).ready(function() {
var $buttonPlus = $('.qtyplus');
var $buttonMin = $('.qtyminus');
var $quantity = $('.qty');

/*For plus and minus buttons*/


$buttonPlus.click(function() {
$quantity.val(parseInt($quantity.val()) + 1).trigger('input');
});

$buttonMin.click(function() {
$quantity.val(Math.max(parseInt($quantity.val()) - 1, 0)).trigger('input');
});
})

You might also like