How To Add Currency Code As Suffix To Prices In WooCommerce

WooCommerceIf you’re looking for a way to modify the price so that your currency code becomes a suffix to your prices in WooCommerce you’ve come to the right code snippet tutorial.

I’m going to show you how to make prices that look like this:

$49

Into this:

$49 AUD

When I was relaunching my site for ThirstyAffiliates we brought the shopping cart experience home with WooCommerce.

During the process I changed my pricing to AUD (Australian Dollars) but because the currency symbol ($) is the same as USD I thought it might cause some confusion if the first place they saw AUD was in PayPal when they got through to process their payment.

I’ve written previously about changing the currency symbol in WooCommerce to include your currency code, but it leaves the prices looking a little unnatural like this “AUD$49”.

The first step is easy enough, create a function to filter the price format in WooCommerce.

WooCommerce makes this ridiculously easy with it’s ample hooks and filters API, we just jump into the “woocommerce_price_format” action.

Here’s the code snippet which will add the suffix to all your prices throughout the site (copy and paste into your functions.php):

function addPriceSuffix($format, $currency_pos) {
	switch ( $currency_pos ) {
		case 'left' :
			$currency = get_woocommerce_currency();
			$format = '%1$s%2$s ' . $currency;
		break;
	}
 
	return $format;
}
 
add_action('woocommerce_price_format', 'addPriceSuffix', 1, 2);

I also wanted the ability to just show the currency code suffix on prices in the cart and checkout pages, leaving my product pages and listings all unaffected.

To do this you need to wrap this add_action with another function that only gets called on those pages.

We use two new actions to call our function to add the action for modifying the price format. Tricky stuff!

Here’s the full code snippet for restricting the formatting to cart and checkout (copy and paste into your functions.php):

function addPriceSuffix($format, $currency_pos) {
	switch ( $currency_pos ) {
		case 'left' :
			$currency = get_woocommerce_currency();
			$format = '%1$s%2$s ' . $currency;
		break;
	}
 
	return $format;
}
 
function addPriceSuffixAction() {
	add_action('woocommerce_price_format', 'addPriceSuffix', 1, 2);
}
 
add_action('woocommerce_before_cart', 'addPriceSuffixAction');
add_action('woocommerce_review_order_before_order_total', 'addPriceSuffixAction');

Hope this helps you, if it does feel free to leave a comment πŸ™‚

Josh Kohlbach

Josh is a software entrepreneur from Brisbane, Australia. He spends most of his time helping e-commerce store owners. This is his personal blog where he shares his thoughts and other tidbits on online business and life in general.

This Post Has 35 Comments

  1. Perfect! I needed a way to show the base currency when using the Currency Converter plugin. The first snippet worked like a charm. Thanks for the post!

    1. Glad it hit the nail on the head Andy πŸ™‚

  2. Awesome. Thanks so much for the post Josh! I have another problem, is there any way to have three decimal places for product prices but have only two for subtotal and total?

  3. How can I get the float value of the price without currency symbol and the currency code?

    1. Hey Jovan,

      In this snippet or just in general?

      In this snippet just replace the format line with
      $format = '%1$s%2$s';

      Otherwise you can use get_product() from the WooCommerce API then on that object use the get_price() function or get_price_html() if you want HTML formatted.

  4. Nice one, you just saved me a lot of time! Thank you.

    1. No worries Pat πŸ™‚

  5. Thanks for this it is almost what I am looking for πŸ™‚

    I applied your code so I got this €9,99 EUR. But is there a way to change it to 9,99 €?

    I tired your advice to Jovan but this did not help.

    Thank you for help.

    1. Hey Miro,

      You probably want to change the setting in the backend of Woo to use right hand side currency, then in the code above use case ‘right’ instead of left. After that follow the rest of the instructions, but alter the format to

      $format = '%2$s%1$s ' . $currency;
  6. Thanks, that first snippet worked perfectly to add AUD to all prices in my store and saved me heaps of time looking it up. Appreciate your article!

    1. No worries Jo, glad you found it helpful.

  7. Hi Josh,

    Thank you very much for the snippets, they are exactly what I needed.

    If I may ask for your help. I have one issue on my checkout page. The AUD is showing after the prices when the page loads, but as soon as the cart totals finish loading (little spinning wheel) the AUD disappears. I am using the first snippet as I want the currency to be shown on all product areas.

    Thank you very much again and I appreciate any help or suggestions.

  8. Thankyou! Just what I was after. I used the second snippet and it worked perfectly.

  9. Thanks! Exactly what I was looking for!

  10. Perfect…exactly what I needed. 1 question…is there an easy way to make this currency symbol appear in a smaller font size than the price itself?

    1. Hi Willy Y,

      This would have to be done in CSS, you could wrap the symbol in a tag with a special class on it though.

      Hope this helps point you in the right direction.

      Josh

    2. Great, the first snippet worked perfectly, just what I needed! But could you elaborate on how I would go about wrapping the currency code (USD, GBP, etc) in a special class in order that I can then make the font size smaller than the main price? Many, many thanks. James

    3. Not to worry, just figured out a way to do this myself. Not sure if this is the BEST way, so please let me know if it can be improved. So I have tweaked the first code snippet to add a custom “currency-suffix” class:

      ——————————————————————-
      function addPriceSuffix($format, $currency_pos) {
      switch ( $currency_pos ) {
      case ‘left’ :
      $currency = ”;
      $currency .= get_woocommerce_currency();
      $currency .= ”;
      $format = ‘%1$s%2$s ‘ . $currency;
      break;
      }

      return $format;
      }

      add_action(‘woocommerce_price_format’, ‘addPriceSuffix’, 1, 2);
      ——————————————————————-

      Then I added this to my custom css:

      /* Make the currency code suffix smaller than the main price */
      .woocommerce-page div.product p.price span.currency-suffix {
      font-size: 15px;
      }

      Cheers, James πŸ™‚

    4. Ok, so posting the code like that didn’t work! Admin, can you email me and I will send you the code, then perhaps you can tweak my reply to show this properly, or perhaps update your post to show it? Cheers, James

  11. Great, you rulz. Works at 100%, thanks for share.

  12. Hi Josh,

    I couldn’t get this to work with the code you provided. Should the above code work as is without using your first method (I have your first method working btw). I also tried changing the WooCommerce setting for right side currency but still no change, the area is just blank without using the first method.

    BTW, I’m using version 2.3.8

    If you could help me out that would be terrific, thanks.

  13. Exactly what I was looking for. Thanks!

  14. Unfortunately it not working or me.
    Probably is the updated Woocommerce

  15. This code took down my site!! please remove this code!

    1. Works OK for me still. Just tested with latest WordPress and WooCommerce.

      Make sure you install in functions.php in your theme. Not some other functions.php.

  16. Great Thanks. This is what I am looking for. The first code is working fine, but the second code did not work for me. How can I place the AUD at the end of the amount instead of at the front.

  17. THANK YOU! The first code worked perfectly for me, much appreciated!

  18. Thanks for this. I added your code to my functions.php file in my child theme and it works to pull up my default CAD currency. However, when switching to USD, the currency code is left off. Would you be able to advise on this? Thanks in advance!

  19. handy as currency is a recommended field for structured data πŸ™‚

  20. Greetings, I would like to ask you that how can I change the currency name suffix from UZS (Global Standard) to Бўм (locally called).
    Feel free to contact me )))

    1. Hello Izzatullo,

      One was is to add a simple conditional directly before the assignment of ‘format’:

      if ( $currency == ‘UZS’ ) { $currency = “Бўм”; }

      You could use a switch if you have multiple of these you’d like to use.

  21. Hi,
    This code always refers the base currency of the store. In a multi-currency store, how can it automatically show the code for the currency used?

  22. This was super helpful and worked for us! Thanks so much, now customers can be sure they’re getting charged Canadian dollars instead of USD.

  23. And how to add a suffix for an arbitrary field? I have a field of minimum retail price (number), I want a currency sign to appear next to a number.

  24. This is not working on product page, only on cart and checkout. Anyone have an idea why?

Leave a Reply

Close Menu