Adding a currency to WooCommerce
WooCommerce has dozens of currencies already included in the plugin, including but not limited to US dollars, euros, British pounds, New Zealand dollars, Russian rubles, South African rands, Egyptian pounds, and the Mexican peso. If you are planning on using any of these currencies, you don't need to add your own currency and can skip this recipe. If you can't find your currency under WooCommerce | Settings | Currency, then you should follow the steps that follow to add it in.
Getting ready
We'll be writing some code to add your custom currency. You'll need a text editor to write the code and an FTP program to upload it to your site once we're done.
How to do it…
We're going to write a very small snippet and add it to WooCommerce. Once it's added, we'll select the currency from the Currency dropdown in the WooCommerce settings pages. The following are the steps that illustrate the process of creating custom currencies:
- Open up your theme's
functions.php
file, located atwp-content/themes/your-theme-name/functions.php
, with your text editor. - At the bottom of the file, we'll need to add two filters to add this currency to WooCommerce. These filters will accept an array with the existing currencies and add another option to the array. The first filter will add the currency to WooCommerce. The second will add the currency symbol. Have a look at the code for these filters:
add_filter( 'woocommerce_currencies', 'add_patricks_currency' ); add_filter( 'woocommerce_currency_symbol', 'add_patricks_currency_symbol', 10, 2 );
- Now let's write the first one, which adds the currency itself. You'll need the name of the currency, which you can append in place of the term
Patrick's Currency
in the snippet that follows. You'll also need the three-character ISO code of the currency, which can be found at http://en.wikipedia.org/wiki/ISO_4217. This can replacePC
in the following code snippet:function add_patricks_currency( $currencies ) { $currencies['PC'] = __( 'Patrick's Currency', 'your-theme-name' ); return $currencies; }
- We're halfway there. Now we need to add the currency symbol. You'll obviously need the currency symbol, which you can use to replace the dollar sign in the snippet that follows. You'll need to use the same currency ISO code you used in the preceding snippet. Let's have a look at the code:
function add_patricks_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'PC': $currency_symbol = 'PC'; break; } return $currency_symbol; }
At this point, WooCommerce should know both your currency and your currency symbol, so it's worth uploading it and making sure we did it right. Upload the file via FTP.
Navigate to the Currency settings by going to WooCommerce | Settings. You should see your new currency in the Currency dropdown:
How it works…
WooCommerce is filled with hooks—places where developers can add or modify the existing code. WooCommerce was smartly built so that anyone can use these hooks to add (or remove) any number of currencies. In this case, we used a filter which is a special type of hook to change the value of something. We changed the contents in an array.
In future recipes, we'll be using actions, which allow completely new programming to fire, and not just changing the value of something.
See also
- We added this snippet to the theme's
functions.php
file. That works, but it's not as bulletproof as putting it in its own WooCommerce plugin. Refer to the preceding recipe, Creating a WooCommerce plugin, for more details.