const plazos = 3 //Cambiar por el número de plazos
const selectores = document.querySelectorAll('.cfvsw-swatches-option')//Selectores Variaciones
const contenedor = document.querySelector('#shortcode-112-74')//Localizado Shortcode
const valInicial = contenedor.children[0].innerText //Valor cuota inicial
const btnReset = document.querySelector('.reset_variations')//Boton Limpiar
function dosDecimales(n) {
let t=n.toString();
let regex=/(\d*.\d{0,2})/;
return t.match(regex)[0];
}
function separadorMiles(x) {
let t=x.toString();
let regex=/(\d*.\d{0,2})/;
let m = t.match(regex)[0];
var parts = m.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return parts.join(".");
}
function ponComa(cadena) {
let t=cadena.toString();
let regex=/(\d*.\d{0,2})/;
let m = t.match(regex)[0];
let parts = m.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".");
let num = parts.join(".");
let regex2 = /\.(?=[^.]*$)/;
if (num.split('.').length > 2){
return num.replace(regex2, ',');
}
return num
}
function tomaPrecio(){
let contenedorPrecio = document.querySelectorAll('#-product-price-53-74')//Contenedor Precio Principal
//console.log(contenedorPrecio)
if(contenedorPrecio[0].children[0].outerText=="")
return contenedor.children[0].innerText= valInicial
precioActual = contenedorPrecio[0].children[0].children[0].children[1].outerText //Valor Precio Principal
precioActual = precioActual.replace("$", "")
precioActual=precioActual.replace(".", "").replace(",", ".")
precioActual=precioActual*1
let cuota = precioActual/plazos
// cuota = dosDecimales(cuota)
//cuota = separadorMiles(cuota)
cuota =ponComa(cuota)
cuota = '$'+cuota
return contenedor.children[0].innerTex= cuota
}
muestraCuota = ()=>{
contenedor.children[0].innerText= tomaPrecio()
}
btnReset.addEventListener('click', () =>{
contenedor.children[0].innerText= valInicial
})
selectores.forEach((selector)=>{
selector.addEventListener('click', ()=>{
contenedor.children[0].innerTex=""
setTimeout(muestraCuota, 700);
})
})