/* =============================================
AMS LIBROS - CRO QUICK WINS JS
============================================= */
(function() {
'use strict'; /* --- 1. TRUST BADGES en páginas de producto --- */
function insertTrustBadges() {
var addToCartBtn = document.querySelector('.single-product .cart');
if (!addToCartBtn) return;
if (document.querySelector('.ams-trust-badges')) return; var badges = document.createElement('div');
badges.className = 'ams-trust-badges';
badges.innerHTML =
'
' +
'' +
'Pago 100% seguro
' +
'
' +
'' +
'Envío a toda la República
' +
'
' +
'' +
'Devolución sin complicaciones
' +
'
' +
'' +
'+10 años en libros infantiles
'; addToCartBtn.parentNode.insertBefore(badges, addToCartBtn.nextSibling);
} /* --- 2. NOTA DE ENVÍO GRATIS bajo el precio en producto --- */
function insertShippingNote() {
var priceBox = document.querySelector('.single-product .price');
if (!priceBox) return;
if (document.querySelector('.ams-shipping-note')) return; // Get current price to compute how much more for free shipping
var priceEl = priceBox.querySelector('ins .woocommerce-Price-amount bdi, .woocommerce-Price-amount bdi');
var priceText = priceEl ? priceEl.textContent.replace(/[^0-9.]/g,'') : '';
var price = parseFloat(priceText) || 0;
var threshold = 500;
var remaining = threshold - price; var note = document.createElement('div');
note.className = 'ams-shipping-note';
if (remaining > 0) {
note.innerHTML = '' +
'¡Agrega $' + remaining.toFixed(0) + ' más para obtener ENVÍO GRATIS 🎉';
} else {
note.innerHTML = '' +
'¡Este pedido califica para ENVÍO GRATIS! 🎉';
}
priceBox.parentNode.insertBefore(note, priceBox.nextSibling);
} /* --- 3. BARRA PROGRESIVA DE ENVÍO GRATIS en el carrito --- */
function insertFreeShippingBar() {
var cartTotals = document.querySelector('.cart_totals');
if (!cartTotals) return;
if (document.querySelector('.ams-free-shipping-bar')) return; // Get subtotal from page
var subtotalEl = document.querySelector('.cart-subtotal .woocommerce-Price-amount bdi, .order-total .woocommerce-Price-amount bdi');
var subtotalText = subtotalEl ? subtotalEl.textContent.replace(/[^0-9.]/g,'') : '0';
var subtotal = parseFloat(subtotalText) || 0;
var threshold = 500;
var pct = Math.min(100, Math.round((subtotal / threshold) * 100));
var remaining = Math.max(0, threshold - subtotal); var bar = document.createElement('div');
bar.className = 'ams-free-shipping-bar';
if (remaining > 0) {
bar.innerHTML = '
🚚 Te faltan $' + remaining.toFixed(0) + ' MXN para ENVÍO GRATIS
' +
'
';
} else {
bar.innerHTML = '
🎉 ¡Felicidades! Tu pedido tiene ENVÍO GRATIS
' +
'
';
}
cartTotals.insertBefore(bar, cartTotals.firstChild);
} /* --- 4. URGENCIA: badge de stock bajo --- */
function insertLowStockBadge() {
var stockEl = document.querySelector('.stock.in-stock');
if (!stockEl) return;
var text = stockEl.textContent;
var match = text.match(/(\d+)/);
if (match) {
var qty = parseInt(match[1]);
if (qty <= 10) {
stockEl.innerHTML = '🔥 ¡Solo quedan ' + qty + ' disponibles! — Pide ya';
stockEl.style.color = '#c62828';
stockEl.style.fontWeight = '700';
}
}
} /* --- 5. TEXTO DEL CHECKOUT EN ESPAÑOL --- */
function translateCheckout() {
var title = document.querySelector('.entry-title, h1.page-title');
if (title && title.textContent.trim().toUpperCase() === 'CHECKOUT') {
title.textContent = 'FINALIZAR COMPRA';
}
var cartTitle = document.querySelector('h1.entry-title, h1.page-title');
if (cartTitle && cartTitle.textContent.trim().toUpperCase() === 'CART') {
cartTitle.textContent = 'MI CARRITO';
}
// Breadcrumb fixes
document.querySelectorAll('.breadcrumb a, .breadcrumb span, .woocommerce-breadcrumb a, nav.woocommerce-breadcrumb').forEach(function(el){
if(el.childNodes.length === 1 && el.childNodes[0].nodeType === 3) {
if (el.textContent.trim() === 'Cart') el.textContent = 'Carrito';
if (el.textContent.trim() === 'Checkout') el.textContent = 'Pago';
if (el.textContent.trim() === 'Home') el.textContent = 'Inicio';
}
});
} /* --- INIT --- */
function init() {
insertTrustBadges();
insertShippingNote();
insertFreeShippingBar();
insertLowStockBadge();
translateCheckout();
} if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();