Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
WooCommerce Cookbook

You're reading from   WooCommerce Cookbook WooCommerce makes it easy to create, design, and manage your own personalized eCommerce store - this WooCommerce tutorial eBook will show you how to get started

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher Packt
ISBN-13 9781784394059
Length 248 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Patrick Rauland Patrick Rauland
Author Profile Icon Patrick Rauland
Patrick Rauland
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. WooCommerce Basics FREE CHAPTER 2. Adding Products 3. Changing the Product Organization 4. Running a Membership Site 5. Setting Up Shipping Methods 6. Getting Paid 7. Modifying the Checkout Process 8. Managing Orders and Taxes 9. WooCommerce Theming 10. Exploring More with WooCommerce Index

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:

  1. Open up your theme's functions.php file, located at wp-content/themes/your-theme-name/functions.php, with your text editor.
  2. 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 );
  3. 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 replace PC in the following code snippet:
    function add_patricks_currency( $currencies ) {
         $currencies['PC'] = __( 'Patrick's Currency', 'your-theme-name' );
         return $currencies;
    }
  4. 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 to do it…

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.
You have been reading a chapter from
WooCommerce Cookbook
Published in: Mar 2015
Publisher: Packt
ISBN-13: 9781784394059
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime