1–5 ₹400
6–10 ₹370
11–20 ₹370
21–50 ₹350
51–100 ₹320
100+ ₹290
Add to Cart
Buy Now
document.addEventListener("DOMContentLoaded", function() {
let sizeCounts = { S:0, M:0, L:0, XL:0, XXL:0, "3XL":0 };
let activeMin = 1;
let finalPricePerUnit = 400;
// Shopify Data
const SHOPIFY_VARIANT_ID = "52217957286181";
const cartBtn = document.getElementById('add-to-cart');
const buyBtn = document.getElementById('buy-now');
const animDot = document.getElementById('cart-anim-dot');
function getPrice(qty) {
if (qty >= 101) return 290;
if (qty >= 51) return 320;
if (qty >= 21) return 350;
if (qty >= 6) return 370;
return 400;
}
function updateLogic() {
let total = Object.values(sizeCounts).reduce((a, b) => a + b, 0);
finalPricePerUnit = getPrice(total);
document.querySelectorAll('.qty-btn').forEach(btn => {
let min = parseInt(btn.dataset.min);
let max = parseInt(btn.dataset.max);
let isActive = total >= min && total <= max;
btn.classList.toggle('active', isActive);
if(isActive) activeMin = min;
});
document.getElementById('count-display').innerText = total + " Pcs";
document.getElementById('final-price').innerText = (total * finalPricePerUnit).toLocaleString('en-IN');
document.getElementById('unit-label').innerText = "₹" + finalPricePerUnit + " per piece";
if (total > 0 && total >= activeMin) {
document.getElementById('status-bar').className = "pm-status-bar status-ready";
document.getElementById('status-text').innerText = "Ready to Checkout ✅";
cartBtn.classList.remove('btn-disabled');
buyBtn.classList.remove('btn-disabled');
} else {
document.getElementById('status-bar').className = "pm-status-bar";
document.getElementById('status-text').innerText = total === 0 ? "Select Sizes" : "Add " + (activeMin - total) + " more";
cartBtn.classList.add('btn-disabled');
buyBtn.classList.add('btn-disabled');
}
let percent = (total / activeMin) * 100;
document.getElementById('progress-fill').style.width = Math.min(percent, 100) + "%";
}
// SHOPYIFY BUY NOW FIX
buyBtn.addEventListener('click', function() {
let total = Object.values(sizeCounts).reduce((a, b) => a + b, 0);
let breakdown = Object.entries(sizeCounts).filter(s => s[1] > 0).map(s => s[0] + ":" + s[1]).join(", ");
buyBtn.innerText = "Redirecting...";
let formData = {
'items': [{
'id': SHOPIFY_VARIANT_ID,
'quantity': total,
'properties': { 'Sizes': breakdown, 'Bulk_Price': '₹' + finalPricePerUnit }
}]
};
fetch('/cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
}).then(() => { window.location.href = '/checkout'; });
});
// ADD TO CART WITH ANIMATION
cartBtn.addEventListener('click', function() {
const btnRect = cartBtn.getBoundingClientRect();
animDot.style.display = 'block';
animDot.style.left = (btnRect.left + btnRect.width / 2) + 'px';
animDot.style.top = btnRect.top + 'px';
animDot.animate([
{ transform: 'translate(0,0) scale(1)', opacity: 1 },
{ transform: 'translate(150px, -600px) scale(0)', opacity: 0 }
], { duration: 800, easing: 'ease-in' }).onfinish = () => { animDot.style.display = 'none'; };
let total = Object.values(sizeCounts).reduce((a, b) => a + b, 0);
fetch('/cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ 'items': [{ 'id': SHOPIFY_VARIANT_ID, 'quantity': total }] })
});
});
document.querySelectorAll('.count-btn').forEach(btn => {
btn.addEventListener('click', () => {
const size = btn.dataset.size;
btn.classList.contains('plus') ? sizeCounts[size]++ : (sizeCounts[size] > 0 ? sizeCounts[size]-- : null);
document.getElementById('val-' + size).innerText = sizeCounts[size];
updateLogic();
});
});
});