// JavaScript to calculate discount and add badge
document.addEventListener("DOMContentLoaded", function () {
// For the homepage
document.querySelectorAll(".product-item").forEach(function (product) {
const beforePriceElement = product.querySelector(".currency-value.before .value");
const afterPriceElement = product.querySelector(".currency-value.after .value");
const productImage = product.querySelector(".product-thumbnail img");
if (beforePriceElement && afterPriceElement && productImage) {
const beforePrice = parseFloat(beforePriceElement.textContent.trim());
const afterPrice = parseFloat(afterPriceElement.textContent.trim());
if (beforePrice > afterPrice) {
const discount = Math.round(((beforePrice - afterPrice) / beforePrice) * 100);
// Create the badge
const badge = document.createElement("div");
badge.className = "discount-badge";
badge.textContent = `-${discount}% تخفيض`;
// Append the badge to the product image container
productImage.parentElement.style.position = "relative"; // Ensure the parent is positioned
productImage.parentElement.appendChild(badge);
}
}
});
// For the product page
document.querySelectorAll(".slick-track .preview-item").forEach(function (previewItem) {
const beforePriceElement = document.querySelector(".currency-value.before .value");
const afterPriceElement = document.querySelector(".currency-value.after .value");
const productImage = previewItem.querySelector("img");
if (beforePriceElement && afterPriceElement && productImage) {
const beforePrice = parseFloat(beforePriceElement.textContent.trim());
const afterPrice = parseFloat(afterPriceElement.textContent.trim());
if (beforePrice > afterPrice) {
const discount = Math.round(((beforePrice - afterPrice) / beforePrice) * 100);
// Create the badge
const badge = document.createElement("div");
badge.className = "discount-badge";
badge.textContent = `-${discount}% تخفيض`;
// Append the badge to the product image container
previewItem.style.position = "relative"; // Ensure the parent is positioned
previewItem.appendChild(badge);