(function() {
'use strict';
console.log(' Sales Display Script Started');
// Check if feature is enabled and we're on a product page
const featureRawValue = `1`;
console.log(' Raw feature value:', featureRawValue);
console.log(' Feature value type:', typeof featureRawValue);
const featureEnabled = featureRawValue == 'true' || featureRawValue == true || featureRawValue == '1';
const isProductPage = !!document.querySelector('.product-single');
console.log(' Feature enabled:', featureEnabled);
console.log(' Is product page:', isProductPage);
if (!featureEnabled || !isProductPage) {
console.log(' Script stopped: Feature disabled or not a product page');
return;
}
// Get product ID from URL
const productId = window.location.pathname.match(/\/p(\d+)$/)?.[1];
console.log(' Product ID from URL:', productId);
if (!productId) {
console.log(' Script stopped: No product ID found');
return;
}
// Parse sales data
let salesData;
try {
salesData = JSON.parse(`[{"p_sales.add_product_sales_num_s":1,"p_sales.product":[{"value":955849207,"label":"الشواحن وملحقاتها","key":955849207}]},{"p_sales.add_product_sales_num_s":60,"p_sales.product":[{"value":955849207,"label":"الشواحن وملحقاتها","key":955849207}]}]`);
console.log(' Sales data parsed:', salesData);
} catch(e) {
console.log(' Script stopped: Failed to parse sales data', e);
return;
}
// Find product in collections
let salesNum = null;
console.log(' Searching for product in collections...');
for (let collection of salesData) {
const products = collection['p_sales.product'];
if (!products) continue;
for (let product of products) {
if (product.value == productId || product.key == productId) {
salesNum = collection['p_sales.add_product_sales_num_s'];
console.log(' Product found! Sales number:', salesNum);
break;
}
}
if (salesNum) break;
}
if (!salesNum) {
console.log(' Script stopped: Product not found in sales data');
return;
}
// Display sales info
const container = document.createElement('div');
container.className = 'cro-product__sales flex justify-center items-center fade-up delay-500 duration-slow ease-smooth animate';
container.innerHTML = `
هذا المنتج تم شراؤه${salesNum}مرة
`;
const target = document.querySelector('.product-single form section salla-add-product-button');
console.log(' Target element found:', !!target);
if (target) {
target.parentNode.insertBefore(container, target.nextSibling);
console.log(' Sales display inserted successfully!');
} else {
console.log('⚠️ Target element not found, display not inserted');
}
// Run when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
}
})();