0

I'm running a currency switching widget on my international woocommerce store. Im working on some jquery to prepend the country code to the price. So instead of the price displaying as $500, it will display as USD$500, and update when the user changes currency.

It works fine up until the click. console.log ( newcurrency ); logs the correct country code on click, but the next line doesn't work.

$(function() {

    // Get current currency
    var currency = $( ".currency_switcher .active" ).text();

    // Add currency before price
    $( ".woocommerce-Price-amount" ).prepend( currency );

    //On click, update currency
    $( ".currency_switcher li a" ).click(function() {

        var newcurrency = $(this).text();

        console.log ( newcurrency );
        $( ".woocommerce-Price-amount" ).prepend( newcurrency );

    });

});

Any thoughts why that line isn't working?

warm__tape
  • 250
  • 4
  • 19
  • My guess is that it is added by ajax or doesn't exist yet. You should check generated html (an option with dev tools in Firefox). Use something like http://stackoverflow.com/a/29279081/1004312 – Christina Feb 24 '17 at 02:52

1 Answers1

1

Solution 1: Add a custom currency / symbol

To add a custom currency in WooCommerce 2.0+, copy and paste this code in your theme functions.php file and swap out the currency code and symbol with your own.

After saving changes, it should be available from your WooCommerce settings.

add_filter( 'woocommerce_currencies', 'add_my_currency' );

function add_my_currency( $currencies ) {
     $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
     return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);

function add_my_currency_symbol( $currency_symbol, $currency ) {
 case 'ABC': $currency_symbol = '$'; break; {
}
 return $currency_symbol;
}

Code is not affected by updates if using a child theme.

Solution 2: View this link

Savan Dholu
  • 810
  • 10
  • 22