Display multiple currencies in WooCommerce - Featured Image

Display multiple currencies in WooCommerce

In Programming, System Administration & Devops by Željko Jagušt2 Comments

On July 15th, 2022, the law on introducing the euro (€) as the official currency in the Republic of Croatia was passed. Euro will become the official currency on January 1st, 2023. The obligation of double reporting will be applied from September 5th, 2022, to December 31st, 2023. During this period, business entities and other obligors in their relations with consumers must state the price and different monetary statements of value in both euros and Croatian kuna.

Prerequisites

If you are doing business in the Republic of Croatia, displaying multiple currencies (both in euro and Croatian kuna) is obligatory for your webshop also. In this article, I will show you how to do it in WooCommerce with a few lines of PHP code. You will, of course, need WooCommerce and a WordPress plugin called Code Snippets. Also, the Croatian government has set a fixed exchange rate of 7,53450 HRK for 1 €, so we will use that as a variable when setting dual prices.

The Code

A bit of technical data first. There is a method/function in the WooCommerce source called get_price_html, which displays the price for every single product on your webshop. And there are many filters you can implement to modify the output of the function, which is precisely what we will do here. To do the real magic, you will need the following block of PHP code:
<?php

function convertCurrency($amount){
    $data = $amount/7.53450;
    return number_format(round($data, 2) ,2);
}

add_filter( 'woocommerce_get_price_html', 'my_custom_price_format', 10, 2 );
function my_custom_price_format( $price, $product) {

    $newprice = $product->price;
    ## Euros
    $price_eur = convertCurrency($newprice, 'HRK','EUR');
    $formatted_price_eur = "<br><span class='price-eur'> &euro;$price_eur</span>";

    ## HRK (formatted price)
    $formatted_price = '<br>'.$price .'';

    // Return the 2 formatted currencies
    return $formatted_price . $formatted_price_eur ;
}
Now let's explain this a bit. First, I defined a function called convertCurrency, which converts the default price in HRK (Croatian kuna) to euros. It also rounds the amount to two decimals:
function convertCurrency($amount){
    $data = $amount/7.53450;
    return number_format(round($data, 2) ,2);
}
Second, I defined a filter which will "override" the default woocommerce_get_price_html function/method and display my custom price function (my_custom_price_format):
add_filter( 'woocommerce_get_price_html', 'my_custom_price_format', 10, 2 );
As the last step, I defined my custom price function to display both prices in HRK and € for every product on my webshop:
function my_custom_price_format( $price, $product) {

    $newprice = $product->price;
    ## Euros
    $price_eur = convertCurrency($newprice, 'HRK','EUR');
    $formatted_price_eur = "<br><span class='price-eur'> &euro;$price_eur</span>";

    ## HRK (formatted price)
    $formatted_price = '<br>'.$price .'';

    // Return the 2 formatted currencies
    return $formatted_price . $formatted_price_eur ;
}

Multiple Currencies Implementation

To put this all to work, you will need to install the Code Snippets plugin first. I presume you know how to install a WordPress plugin, so once you install and activate the plugin, please click on Snippets -> All Snippets from your main menu:
Display multiple currencies in WooCommerce - All Snippets
Select All Snippets from main menu
Once you're there, please click on Add New button in the top-left corner of the screen:
Display multiple currencies in WooCommerce - Add new Snippet
Add new snippet
On the following screen, give your snippet a proper title and paste the code from above to a code designated space. Make sure to select the Run snippet everywhere option. You can also give your snippet a description and tag it nicely. Once done, click on the Save Changes and Activate button. 
Display multiple currencies in WooCommerce - Snippet Content
Add new snippet
And voila, with this complete, all your product prices are now displayed in Croatian kuna and euro currency. Thank you for reading, and please comment if you have a similar solution for other currencies and variable exchange rates.

Share if you like. Thank you in advance!


You may also like


Comments

  1. Hello!

    A fine solution! Any chance you can add it for the cart and checkout as well?

    Cheers!

    1. Author

      Hello Klemen,

      Thanks for writing in. In my article, I have a basic example of how one can display prices in multiple currencies (I’m not much of a PHP coder). In the time since I wrote this article, I found two plugins that fully comply with the current Croatian conversion law:

      https://bit.ly/3RQm66z -> Display the lowest price in the last 30 days
      https://bit.ly/3RJzcmx -> Display informative price in €

      I am using both on several websites, and they work fine. I hope they will do the same for you 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.