File: /var/www/kevin-demo/wp-content/themes/fitmencook/js/single-product-toggle.js
// const items = document.querySelectorAll(".item");
// items.forEach(function (item) {
// item.addEventListener("click", function () {
// const icon = item.querySelector("ion-icon.product-icon");
// const isOpen = item.classList.toggle("open");
// if (icon) {
// icon.setAttribute("name", isOpen ? "remove-outline" : "add-outline");
// }
// });
// });
// cart
const isWellness = document.querySelector(".product-type-wellness") !== null;
const items = document.querySelectorAll(".item");
items.forEach((item) => {
item.addEventListener("click", () => {
// 1) zatvori sve ostale
items.forEach((other) => {
if (other !== item) {
other.classList.remove("open");
const otherIcon = other.querySelector("ion-icon.product-icon");
if (otherIcon) otherIcon.setAttribute("name", "add-outline");
}
});
// 2) toggle na kliknutom
const icon = item.querySelector("ion-icon.product-icon");
const isOpen = item.classList.toggle("open");
if (icon)
icon.setAttribute("name", isOpen ? "remove-outline" : "add-outline");
});
});
(function () {
// thumbnail → main image swap
const mainImg = document.getElementById("main-product-image");
document.querySelectorAll(".thumbnail-image").forEach((thumb) => {
thumb.addEventListener("click", () => {
// swap src & alt
mainImg.src = thumb.src;
mainImg.alt = thumb.alt || mainImg.alt;
});
});
})();
// Thumbnail gallery carousel navigation
(function () {
const gallery = document.querySelector(".thumbnail-gallery");
const prevBtn = document.querySelector(".thumb-nav-prev");
const nextBtn = document.querySelector(".thumb-nav-next");
if (!gallery || !prevBtn || !nextBtn) return;
const scrollAmount = 100; // pixels to scroll
prevBtn.addEventListener("click", () => {
gallery.scrollBy({ top: -scrollAmount, behavior: "smooth" });
});
nextBtn.addEventListener("click", () => {
gallery.scrollBy({ top: scrollAmount, behavior: "smooth" });
});
})();
// cart
const priceEl = document.querySelector(".discounted-price");
const regularPriceEl = document.querySelector(".regular-price");
const plusBtn = document.querySelector(".quantity-plus");
const minusBtn = document.querySelector(".quantity-minus");
const qtyInput = document.getElementById("quantity");
const totalEl = document.getElementById("total-price");
const hiddenQty = document.getElementById("quantity-value");
const hiddenCalc = document.getElementById("calculated-price");
const priceWrapper = document.querySelector(".product-price-wrapper");
let basePrice = parseFloat(priceEl?.getAttribute("data-base-price")) || 0;
let currency = priceEl?.getAttribute("data-currency") || "";
let regularPrice = regularPriceEl ? parseFloat(regularPriceEl.getAttribute("data-regular-price")) || basePrice : basePrice;
// Read discount settings from data attribute (ACF global settings)
const enableOffer = priceWrapper?.getAttribute("data-enable-offer") === "1";
const discountQty = parseInt(priceWrapper?.getAttribute("data-discount-qty")) || 3;
const discountPct = parseInt(priceWrapper?.getAttribute("data-discount-pct")) || 10;
const discountMultiplier = (100 - discountPct) / 100; // npr. 10% off = 0.9
function formatPrice(amount) {
return currency + amount.toFixed(2);
}
function updatePriceDisplay() {
// preuzmi količinu iz inputa
let qty = parseInt(qtyInput.value) || 1;
if (qty < 1) {
qtyInput.value = 1;
qty = 1;
}
if (hiddenQty) {
hiddenQty.value = qty;
}
let fullPrice = basePrice * qty; // puna cena bez popusta
let newPrice = fullPrice;
if (hiddenCalc) {
hiddenCalc.value = newPrice;
}
// Ažuriraj regular cenu (precrtanu) ako postoji - za wellness i ostale proizvode
if (regularPriceEl && regularPrice) {
const regularTotalPrice = regularPrice * qty;
regularPriceEl.textContent = formatPrice(regularTotalPrice);
}
// Primeni popust ako je uključen i količina je >= discount quantity
if (!isWellness && enableOffer && qty >= discountQty) {
newPrice = fullPrice * discountMultiplier;
// Ažuriraj precrtanu cenu da pokazuje punu cenu za trenutnu količinu
if (totalEl) {
totalEl.textContent = formatPrice(fullPrice);
totalEl.style.opacity = "1";
}
if (hiddenCalc) {
hiddenCalc.value = newPrice;
}
} else {
if (totalEl) totalEl.style.opacity = "0";
}
if (priceEl) priceEl.textContent = formatPrice(newPrice);
}
if (plusBtn && minusBtn && qtyInput) {
plusBtn.addEventListener("click", () => {
let current = parseInt(qtyInput.value) || 0;
const step = parseInt(qtyInput.getAttribute("step")) || 1;
qtyInput.value = current + step;
updatePriceDisplay();
});
minusBtn.addEventListener("click", () => {
let current = parseInt(qtyInput.value) || 0;
const step = parseInt(qtyInput.getAttribute("step")) || 1;
const min = parseInt(qtyInput.getAttribute("min")) || 1;
if (current - step >= min) {
qtyInput.value = current - step;
} else {
qtyInput.value = min;
}
updatePriceDisplay();
});
qtyInput.addEventListener("change", () => {
updatePriceDisplay();
});
}
updatePriceDisplay();
// select option:
document.addEventListener("DOMContentLoaded", function () {
// Pronađemo gumb i padajuću listu
const trigger = document.getElementById("fmc-picker-trigger");
const dropdown = document.getElementById("fmc-picker-dropdown");
if (!trigger || !dropdown) {
// Ako ne postoje ti elementi na stranici, napustimo skriptu
return;
}
// Funkcija za otvaranje/zatvaranje dropdowna
function toggleDropdown(e) {
e.preventDefault(); // sprječavamo default ponašanje buttona
dropdown.classList.toggle("open");
}
// Klik na gumb otvara/zatvara listu
trigger.addEventListener("click", toggleDropdown);
// Ako se klikne negdje izvan samog triggera i izvan dropdowna, zatvorimo ga
document.addEventListener("click", function (e) {
// Ako kliknuti element NIJE unutar triggera, i NIJE unutar dropdowna, uklonimo "open"
if (!trigger.contains(e.target) && !dropdown.contains(e.target)) {
dropdown.classList.remove("open");
}
});
// Svi <a> linkovi unutar dropdowna već imaju value (permalink).
// Ostavit ćemo default ponašanje <a>, pa ne trebamo dodatni JS za navigaciju.
// (Ako želiš nekakav loading spinner ili slično, možeš ih ovdje presresti.)
});