document.addEventListener("DOMContentLoaded", function () {
function replaceRemoveText() {
const removeLinks = document.querySelectorAll('.remove.remove_from_cart_button');
if (!removeLinks.length) return;
removeLinks.forEach(link => {
if (link.textContent.trim() === '×') {
link.textContent = 'Remove';
}
});
}
// Delay to allow mini cart to load
setTimeout(replaceRemoveText, 800);
// Observe AJAX updates safely
const observer = new MutationObserver((mutationsList, obs) => {
replaceRemoveText();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
document.addEventListener("DOMContentLoaded", function () {
function enhanceMiniCartQuantity() {
const cartItems = document.querySelectorAll('.woocommerce-mini-cart-item');
cartItems.forEach(item => {
const span = item.querySelector('.quantity');
if (!span || span.querySelector('.qty-wrapper')) return;
const removeBtn = item.querySelector('a.remove');
const itemKey = removeBtn?.getAttribute('data-cart_item_key');
if (!itemKey) return;
let rawQty = span.innerText.trim().split('×')[0].trim();
const currentQty = parseInt(rawQty) || 1;
// ✅ Look for unit price within the item, not just in .quantity
const priceEl = item.querySelector('.woocommerce-Price-amount');
const unitPrice = priceEl ? parseFloat(priceEl.textContent.replace(/[^0-9.]/g, '')) : 0;
const wrapper = document.createElement('div');
wrapper.className = 'qty-wrapper';
wrapper.style.display = 'flex';
wrapper.style.alignItems = 'center';
wrapper.style.gap = '6px';
const minus = document.createElement('button');
minus.textContent = '−';
minus.style.cssText = 'padding:2px 6px; cursor:pointer;';
const plus = document.createElement('button');
plus.textContent = '+';
plus.style.cssText = 'padding:2px 6px; cursor:pointer;';
const input = document.createElement('input');
input.type = 'number';
input.min = 1;
input.value = currentQty;
input.style.cssText = 'width:60px; text-align:center;';
const totalPriceEl = document.createElement('div');
totalPriceEl.className = 'line-total-price';
totalPriceEl.style.cssText = 'margin-top: 6px; font-weight: bold;';
totalPriceEl.textContent = `= $${(unitPrice * currentQty).toFixed(2)}`;
function updateDisplayedPrice(newQty) {
totalPriceEl.textContent = `= $${(unitPrice * newQty).toFixed(2)}`;
}
minus.onclick = () => {
if (parseInt(input.value) > 1) {
input.value = parseInt(input.value) - 1;
updateQty(itemKey, input.value);
updateDisplayedPrice(input.value);
}
};
plus.onclick = () => {
input.value = parseInt(input.value) + 1;
updateQty(itemKey, input.value);
updateDisplayedPrice(input.value);
};
input.onchange = () => {
if (parseInt(input.value) >= 1) {
updateQty(itemKey, input.value);
updateDisplayedPrice(input.value);
}
};
wrapper.appendChild(minus);
wrapper.appendChild(input);
wrapper.appendChild(plus);
span.textContent = '';
span.appendChild(wrapper);
span.appendChild(totalPriceEl);
});
}
function updateQty(itemKey, qty) {
fetch('/wp-admin/admin-ajax.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
action: 'update_cart_item',
cart_item_key: itemKey,
quantity: qty
})
})
.then(async res => {
const text = await res.text();
try {
const data = JSON.parse(text);
if (data.success && data.data && data.data.fragments) {
for (const selector in data.data.fragments) {
const el = document.querySelector(selector);
if (el) el.innerHTML = data.data.fragments[selector];
}
jQuery(document.body).trigger('wc_fragments_loaded');
if (window.location.pathname === '/cart/') {
window.location.reload();
} else {
enhanceMiniCartQuantity();
}
} else {
alert("❌ Failed to update cart.");
console.error(data);
}
} catch (e) {
alert("❌ Invalid server response.");
console.error("Invalid JSON from server:", text);
}
})
.catch(err => {
console.error("❌ AJAX Error:", err);
alert("❌ AJAX failed. See console.");
});
}
enhanceMiniCartQuantity();
jQuery(document.body).on('wc_fragments_loaded wc_fragments_refreshed', function () {
enhanceMiniCartQuantity();
});
});