Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
<div class="currency-switcher"> <select id="currency-select"> <option value="USD">USD</option> <option value="PKR">PKR</option> </select> </div>
select { background-size: 4px 4px, 4px 4px; border-radius: 4px; }
document.addEventListener('DOMContentLoaded', function() { const currencySelect = document.getElementById('currency-select'); // Conversion rates (you can update this based on your latest rates) const conversionRates = { USD: 1, // Base currency PKR: 285, // Conversion rate from USD to PKR (example) }; // Function to update prices function updatePrices(currency) { const elements = document.querySelectorAll('.woocommerce-Price-amount, p, label, .options-wrapper li label, .brxe-ltoycd brxe-text-basic'); elements.forEach(element => { let priceText = element.innerText || element.textContent; // Skip non-price elements like "25% OFF" if (priceText.includes('% OFF')) return; // Match prices with currency symbols or numbers let matches = priceText.match(/(\$|€|₹|\d+(\.\d{1,2})?)/g); // Match currency symbols and prices if (matches) { matches.forEach(match => { // Remove the currency symbol and parse the number let price = parseFloat(match.replace(/[^\d.-]/g, '')); // If a valid price is found if (!isNaN(price)) { // Convert the price let convertedPrice = price * conversionRates[currency]; // Format the price back with the appropriate currency symbol let formattedPrice; if (currency === 'USD') { formattedPrice = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(convertedPrice); } else if (currency === 'PKR') { formattedPrice = `PKR ${new Intl.NumberFormat('en-IN').format(convertedPrice)}`; // Use Indian locale for PKR formatting } // Remove the existing currency symbol to avoid duplication element.innerHTML = element.innerHTML.replace(match, formattedPrice); } }); } }); } // Reload page when the currency changes currencySelect.addEventListener('change', function() { let selectedCurrency = currencySelect.value; localStorage.setItem('selectedCurrency', selectedCurrency); // Store the selected currency // Reload the page to properly reapply the conversion with the selected currency window.location.reload(); }); // Load the currency from localStorage const storedCurrency = localStorage.getItem('selectedCurrency') || 'USD'; currencySelect.value = storedCurrency; updatePrices(storedCurrency); // Apply the selected currency after reload });
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
1 comment
A WordPress Commenter
Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.
Comments are closed.