WooCommerce Shopping Website Currency Converter Code

PHP Code:


/**
 * WooCommerce Product Currency Converter
 * Display-only converter. Store and checkout currency remain unchanged.
 *
 * Add using the Code Snippets plugin.
 */

add_action( 'woocommerce_single_product_summary', 'pm_currency_converter_display', 11 );

function pm_currency_converter_display() {

    if ( ! is_product() ) {
        return;
    }

    $currencies = array(
        'INR' => 'Indian Rupee (INR)',
        'USD' => 'US Dollar (USD)',
        'EUR' => 'Euro (EUR)',
        'GBP' => 'British Pound (GBP)',
        'AED' => 'UAE Dirham (AED)',
        'CAD' => 'Canadian Dollar (CAD)',
    );

    echo '<div class="pm-currency-converter">';
    echo '<label for="pm-currency-select">Convert Price:</label> ';
    echo '<select id="pm-currency-select">';

    foreach ( $currencies as $code => $name ) {
        echo '<option value="' . esc_attr( $code ) . '">'
            . esc_html( $name ) .
        '</option>';
    }

    echo '</select>';
    echo '<div id="pm-converted-price" aria-live="polite"></div>';
    echo '<small>Approximate converted price. Final price is in store currency.</small>';
    echo '</div>';
    ?>

    <style>
        .pm-currency-converter {
            margin: 15px 0;
            padding: 14px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background: #f9f9f9;
        }

        .pm-currency-converter label {
            font-weight: 600;
            margin-right: 8px;
        }

        #pm-currency-select {
            padding: 8px;
            max-width: 100%;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        #pm-converted-price {
            margin-top: 12px;
            font-size: 20px;
            font-weight: 700;
        }

        .pm-currency-converter small {
            display: block;
            margin-top: 6px;
            color: #666;
        }
    </style>

    <script>
    (function () {
        const rates = {
            INR: 1,
            USD: 0.012,
            EUR: 0.011,
            GBP: 0.0095,
            AED: 0.044,
            CAD: 0.016
        };

        const select = document.getElementById('pm-currency-select');
        const output = document.getElementById('pm-converted-price');

        if (!select || !output) return;

        function getOriginalPrice() {
            const priceElement = document.querySelector(
                '.single-product .summary .price'
            );

            if (!priceElement) return null;

            // Read the regular displayed price, excluding currency symbols.
            const amount = priceElement.querySelector(
                '.woocommerce-Price-amount'
            );

            if (!amount) return null;

            let text = amount.textContent.replace(/[^\d.,]/g, '');

            // WooCommerce INR prices normally use commas as thousands separators.
            text = text.replace(/,/g, '');

            const price = parseFloat(text);

            return Number.isFinite(price) ? price : null;
        }

        function convertPrice() {
            const originalPrice = getOriginalPrice();

            if (originalPrice === null) {
                output.textContent = '';
                return;
            }

            const currency = select.value;
            const converted = originalPrice * rates[currency];

            output.textContent = new Intl.NumberFormat('en-IN', {
                style: 'currency',
                currency: currency,
                maximumFractionDigits: 2
            }).format(converted);
        }

        select.addEventListener('change', convertPrice);

        // Update when a customer selects a variable product option.
        if (window.jQuery) {
            jQuery(document).on(
                'found_variation reset_data',
                'form.variations_form',
                function () {
                    setTimeout(convertPrice, 100);
                }
            );
        }

        convertPrice();
    })();
    </script>

    <?php
}

Currencies Cournty Code:

$currencies = array(
    'INR' => 'India - Indian Rupee (INR)',
    'USD' => 'USA - US Dollar (USD)',
    'EUR' => 'Europe - Euro (EUR)',
    'GBP' => 'United Kingdom - Pound (GBP)',
    'AED' => 'UAE - Dirham (AED)',
    'CAD' => 'Canada - Dollar (CAD)',
    'AUD' => 'Australia - Dollar (AUD)',
    'JPY' => 'Japan - Yen (JPY)',
    'CNY' => 'China - Yuan (CNY)',
    'SGD' => 'Singapore - Dollar (SGD)',
    'SAR' => 'Saudi Arabia - Riyal (SAR)',
    'QAR' => 'Qatar - Riyal (QAR)',
    'KWD' => 'Kuwait - Dinar (KWD)',
    'BDT' => 'Bangladesh - Taka (BDT)',
    'PKR' => 'Pakistan - Rupee (PKR)',
    'NPR' => 'Nepal - Rupee (NPR)',
    'LKR' => 'Sri Lanka - Rupee (LKR)',
    'THB' => 'Thailand - Baht (THB)',
    'MYR' => 'Malaysia - Ringgit (MYR)',
    'CHF' => 'Switzerland - Franc (CHF)',
    'NZD' => 'New Zealand - Dollar (NZD)',
    'ZAR' => 'South Africa - Rand (ZAR)',
);

Price Code:


    const rates = {
    INR: 1,
    USD: 0.012,
    EUR: 0.011,
    GBP: 0.0095,
    AED: 0.044,
    CAD: 0.016,
    AUD: 0.018,
    JPY: 1.80,
    CNY: 0.086,
    SGD: 0.016,
    SAR: 0.045,
    QAR: 0.044,
    KWD: 0.0037,
    BDT: 1.45,
    PKR: 3.35,
    NPR: 1.60,
    LKR: 3.60,
    THB: 0.40,
    MYR: 0.053,
    CHF: 0.010,
    NZD: 0.020,
    ZAR: 0.21
};

Leave a Comment

Prove your humanity: 2   +   5   =