// ============================================ // دادههای محصولات (برای جستجو و سبد خرید) // ============================================ let productsData = []; let cart = []; let selectedWeightPrice = 295000; // ============================================ // توابع سبد خرید // ============================================ function quickAddToCart(id) { // اگر محصول در دیتا نبود، از طریق Ajax واکشی کن fetch(motayeb_ajax.ajax_url + '?action=get_product_data&id=' + id + '&nonce=' + motayeb_ajax.nonce) .then(response => response.json()) .then(data => { if (data.success) { const p = data.data; const existing = cart.find(x => x.id === p.id); if (existing) { existing.qty++; } else { cart.push({ ...p, qty: 1 }); } updateCartUI(); showToast(p.name + ' به سبد خرید اضافه شد', 'success'); } }) .catch(() => { // اگر Ajax کار نکرد، از دادههای mock استفاده کن const mockProduct = { id: id, name: 'محصول شماره ' + id, price: 100000, img: 'default-product' }; const existing = cart.find(x => x.id === mockProduct.id); if (existing) { existing.qty++; } else { cart.push({ ...mockProduct, qty: 1 }); } updateCartUI(); showToast(mockProduct.name + ' به سبد خرید اضافه شد', 'success'); }); } function addToCartDetail(productId) { const qty = parseInt(document.getElementById('product-qty')?.textContent || 1); // از طریق Ajax محصول رو واکشی کن fetch(motayeb_ajax.ajax_url + '?action=get_product_data&id=' + productId + '&nonce=' + motayeb_ajax.nonce) .then(response => response.json()) .then(data => { if (data.success) { const p = data.data; const existing = cart.find(x => x.id === p.id); if (existing) { existing.qty += qty; } else { cart.push({ ...p, qty: qty }); } updateCartUI(); showToast(p.name + ' به سبد خرید اضافه شد', 'success'); } }) .catch(() => { // Mock const mockProduct = { id: productId, name: 'محصول شماره ' + productId, price: 100000, img: 'default-product' }; const existing = cart.find(x => x.id === mockProduct.id); if (existing) { existing.qty += qty; } else { cart.push({ ...mockProduct, qty: qty }); } updateCartUI(); showToast(mockProduct.name + ' به سبد خرید اضافه شد', 'success'); }); } function removeFromCart(id) { cart = cart.filter(x => x.id !== id); updateCartUI(); } function updateCartQty(id, delta) { const item = cart.find(x => x.id === id); if (!item) return; item.qty += delta; if (item.qty <= 0) { removeFromCart(id); return; } updateCartUI(); } function updateCartUI() { const count = cart.reduce((s, i) => s + i.qty, 0); const total = cart.reduce((s, i) => s + i.price * i.qty, 0); // Badge ['cart-count', 'mobile-cart-count', 'cart-drawer-count'].forEach(id => { const el = document.getElementById(id); if (el) { if (id === 'cart-drawer-count') { el.textContent = count; } else { if (count > 0) { el.textContent = count; el.classList.remove('hidden'); } else { el.classList.add('hidden'); } } } }); // Drawer items const listEl = document.getElementById('cart-items-list'); const emptyEl = document.getElementById('cart-empty'); const footerEl = document.getElementById('cart-drawer-footer'); if (!listEl) return; if (cart.length === 0) { listEl.classList.add('hidden'); if (emptyEl) emptyEl.classList.remove('hidden'); if (footerEl) footerEl.classList.add('hidden'); } else { listEl.classList.remove('hidden'); if (emptyEl) emptyEl.classList.add('hidden'); if (footerEl) footerEl.classList.remove('hidden'); const totalEl = document.getElementById('cart-drawer-total'); if (totalEl) totalEl.textContent = formatPrice(total) + ' تومان'; listEl.innerHTML = cart.map(i => `
سبد خرید خالی است
'; } else { checkoutItems.innerHTML = cart.map(i => `
محصولی یافت نشد
'; } }) .catch(() => { // Mock results results.innerHTML = 'خطا در جستجو. لطفاً دوباره تلاش کنید.
'; }); } //زوم تصویر function changeImage(src, btn) { const mainImg = document.getElementById('main-product-img'); if (!mainImg) return; // 1. تغییر تصویر اصلی mainImg.src = src; // 2. آپدیت ویژگیهای ضروری برای زوم و پاپآپ ووکامرس mainImg.setAttribute('data-large_image', src); mainImg.setAttribute('data-src', src); // 3. مهمترین بخش برای پاپآپ: آپدیت لینک والد ( اطراف عکس) // ووکامرس برای باز کردن پاپآپ از روی href این لینک استفاده میکند const parentLink = mainImg.closest('a'); if (parentLink) { parentLink.href = src; } // 4. بروزرسانی کلاس فعال روی thumbnails document.querySelectorAll('.gallery-thumb').forEach(t => t.classList.remove('active')); if (btn) btn.classList.add('active'); // 5. بازسازی مجدد Zoom (با متد مقاومتر) if (typeof jQuery !== 'undefined' && typeof jQuery.fn.zoom !== 'undefined') { const $mainImg = jQuery(mainImg); // والد بلافصل عکس را هدف قرار میدهیم (معمولاً یک