Add filters to default country,state, and currency on WooCommerce


WooCommerce is awesome plugin, you can use it to make e-commerce on your website. But sometime we need add some country, state, or currency into this plugin. We can edit manually with edit some file in this plugin but if we updated the plugin the file will be replaced, and all customization what we have done is gone.

Now, i will be show you how to add currency and country with filter in this plugin. Remember that everything is filtered. Woo is using the woocommerce_currency_symbol filter for filtered the currency and woocommerce_countries filter for filtered country in woocommerce.php. To add currencies to that list, you’d use:

function add_my_currency_symbol( $currency_symbol, $currency ) {     if($currency == "IDR") {         $currency_symbol = 'Rp';     }     return $currency_symbol; } add_filter( 'woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2 ); 

Whenever Woo calls their function, they pass the currency symbol through a filter before returning it. You’re just hooking on to that filter, checking to see if the currency matches your custom value, and returning a custom symbol if so. I used Rupiah here as an example, but substitute whatever it is you need.

To add your currency to the settings page, you’ll hook into the woocommerce_currencies filter:

function add_my_currency( $currencies ) {     
$currencies["IDR"] = 'Rupiah';     
return $currencies; 
} 
add_filter( 'woocommerce_currencies', 'add_my_currency', 10, 1 ); 

To add country  to that list, you’d use:

function add_my_country( $country ) {     
$country["NC"] = 'New Country'; 
return $country; 
} 
add_filter( 'woocommerce_countries', 'add_my_country', 10, 1 );

To add states  to that list, you’d use:

function add_my_states( $states ) {     
$states["ID"] = array( 'BA' => __('Bali', 'woocommerce') ,'JB' => __('Jawa Barat', 'woocommerce')); 
return $states; 
} 
add_filter( 'woocommerce_states', 'add_my_states', 10, 1 );

Woo is adding an array of currencies to their options panel. All this does is look at the array, add your custom currency, then return it so the function can operate as normal.

Where to put this code

To use these filters, you’ll have to write a that’s code in your function.php at your theme file. When you update WooCommerce, so long as they don’t change their filter names, you shouldn’t lose any customizations.

I hope that can help you to make your website more awesome, keep on fire yeah :)

About these ads

29 thoughts on “Add filters to default country,state, and currency on WooCommerce

    • Anang Pratika says:

      Hi Stan,

      try this one
      function add_my_states( $states ) {
      $states['JKT']["NWST"] = ‘New States';

      return $states;
      }
      add_filter( ‘woocommerce_states’, ‘add_my_states’, 10, 1 );

  1. brian says:

    .. so to add a country we are ONLY modifying the function.php file and nothing else. The article is great but where to put the code additions is … not clear as you mention several files. I’m trying to add Argentina, can you please elaborate : ) thanks!

    • Anang Pratika says:

      hi brian,

      thanks for your comment, okay please try this code to put your country
      function dd_my_country( $country ) {
      $country["AR"] = ‘Argentina';
      return $country; }
      add_filter( ‘woocommerce_countries’, ‘add_my_country’, 10, 1 );

  2. brian says:

    and that’s anywhere…. in function.php

    You have three sections of PHP code, I ask where we place those sections, and you respond with 1 section and do not confirm where I place it…

  3. Hi there thank you very much for your help, i succesfully add a custom currency with this code

    function add_my_currency( $currencies ) {
    $currencies["COP"] = ‘Peso Colombiano ($COP)';
    return $currencies;
    }
    add_filter( ‘woocommerce_currencies’, ‘add_my_currency’, 10, 1 );

    now i have a question, how can i add a custom currency symbol like this $COP for woocomerce to display, because it still show me this £ in the shop

    • Stanislav says:

      Here you are:

      add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);

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

      • hi there, thank you very much for your help, i tried with the you gave me, i replace the ABC with COP and Dhs with $COP and i got this
        Use of undefined constant ‘add_my_currency_symbol’ – assumed ‘‘add_my_currency_symbol’’ in functions.php on line 25.
        what am i doing wrong? please help me!

      • Stanislav says:

        Hi again…. I’m sorry, the mistake is mine…
        This is your code:

        ############################################################
        add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );

        function add_my_currency( $currencies ) {
        $currencies['COP'] = __( ‘Colombian pesos (COP)’, ‘woocommerce’ );
        return $currencies;
        }

        add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);

        function add_my_currency_symbol( $currency_symbol, $currency ) {
        switch( $currency ) {
        case ‘COP': $currency_symbol = ‘COP'; break;
        }
        return $currency_symbol;
        }
        ############################################################

  4. Eric says:

    I followed the instruction here, my currency symbol did not appear and when I copied the code and edited it, my site crash.

    Don’t worry is a new site.

    I need help, I am a Nigerian, based in Abuja, Nigeria’s capital.

    My country’s currency symbol is: N

    Why my country’s currency name is: Naira

    Please, help me fix this,

    • Anang Pratika says:

      Hi Eric,
      Please try put this code into function.php on your themes

      add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );

      function add_my_currency( $currencies ) {
      $currencies['NGN'] = __( ‘Naira (NGN)’, ‘woocommerce’ );
      return $currencies;
      }

      add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);

      function add_my_currency_symbol( $currency_symbol, $currency ) {
      switch( $currency ) {
      case ‘NGN’: $currency_symbol = ‘NGN’; break;
      }
      return $currency_symbol;
      }

  5. Eric says:

    Thank you for doing all this just for me, I am grateful,
    God Bless.

    Having said that, it important I let you know that adding the above code to A DEMO site (for testing purpose): failed.
    Theme Functions
    (functions.php)
    Crash the Demo website before I re-uploaded wordpress again and added it to:

    woocommerce’s editor > woocommerce/woocommerce-functions.php
    And I got the same negative result.

    This is what normally shows up:

    Parse error: syntax error, unexpected T_STRING in /home/udoka/public_html/wootesting/wp-content/themes/woostore/functions.php on line 40

    I strongly believe that I am not doing it the right way,

    Sorry to bother you again,
    Please can you do a short video for me and post it on youtube on ‘How To Installed The Code’

    Sorry again for bothering you.

    Thanks in advance for helping out and God bless

    • Anang Pratika says:

      Hi Eric,

      Sorry for late to response your comment, put that your code into your function theme file, not into your plugin file. you can find the into wp-content/themes/yourtheme/functions.php.

      If you still need my help, you can sent me your admin account and ftp account to http://anangpratika.wordpress.com/contact-me-2/, cause i still don’t have some time to make video tutorial.

      Cheers,

  6. Joan Manuel says:

    Hi there i put the code in the functions.php file and im getting the error: Parse error: syntax error, unexpected T_STRING in /home/xxxxx/wp-content/themes/wpflexishoptwo/functions.php on line 11 what should i do, tx in advance

    • Anang Pratika says:

      Hi Tantea,

      Sorry to late reply your comment, to adding Indian rupee to your theme, please open your function.php of your theme (wp-content/themes/your-theme/function.php). And copy this code into your function.php

      add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );

      function add_my_currency( $currencies ) {
      $currencies['INR'] = __( ‘Indian Rupee (INR)’, ‘woocommerce’ );
      return $currencies;
      }

      add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);

      function add_my_currency_symbol( $currency_symbol, $currency ) {
      switch( $currency ) {
      case ‘INR’: $currency_symbol = ‘INR’; break;
      }
      return $currency_symbol;
      }

      I hope that’s can help you. :)

  7. my way more easier—Open woocommerce/woocommerce-core-functions.php in wordpress plugin editor.theres a switch function–” function get_woocommerce_currency_symbol( $currency = ” ) ” just change the case condition like–“case ‘EUR’ : $currency_symbol = ‘Rs'; break;”..ha ha ha

    • Anang Pratika says:

      Hi abhishek,

      If you do with that way, you can’t update your woocommerce plugin. Cause if there is new version of woocommerce and you updating it, your customization will be removed and back it to the original. Am i right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s