Top 5 Programming Questions and Answers on WooCommerce Multi Currency - Part 1

February 9, 2017,
Top 5 Programming Questions and Answers on WooCommerce Multi Currency - Part 1
WooCommerce Multi Currency Extension is a powerful tool for any eCommerce website offering their services and products in more than one regions or countries.  It is basically developed to provide product prices to customers of their choice. Using this extension, store owners can set the default currency for different geographical locations based on GEO IP.  Once you define individual rules for different locations, WooCommerce currency switcher automatically detects the currency of that particular country and converts product prices of the website to the particular local currency.  As this is a must have extension for your e-store, it becomes essential to learn all about its problems that end users/developers face at the time of installation or configuration this extension. FMEAddons has picked up this week’s hot questions and answers give by the top community developers.

Question No1

The user has an online shop based on WooCommerce and the Multilanguage plugin with activities multi-currency, Currently, the currency switcher works perfectly. The user is asking is there any to get the current currency in PHP?

Answer

This is simple, use get_woocommerce_currency() to get the currency code of any currency i.e. USD, EUR

Question No2

How to switch between different payment options with choosing a currency in WooCommerce The user wants to add an option at the store checkout page to select the currency for payment (USD or RS) and based on the payment gateway option, such as PayPal, Stripe, or WooCommerce Apple pay, etc. i.e. PayPal if USD is selected PAYMONEY if RS and disable other payment gateways. How to do this?

Answer

This function might be helpful to resolve the issue
function woocs_filter_gateways($gateway_list)
{
    global $WOOCS;
    $exclude = array(
        'paypal' => array('INR'), //do not show paypal gate if current currency is INR
        'payuindia' => array('USD')//do not show payumoney gate if current currency is USD
);
//***
foreach ($exclude as $gateway_key => $currencies)
{
    if (isset($gateway_list[$gateway_key]) AND in_array($WOOCS->current_currency, $currencies))
    {
        unset($gateway_list[$gateway_key]);
    }
}

return $gateway_list;
}

Question No3

How to show currency switcher widget in a drop down menu next to 'Add to Cart' button on the single product page in WooCommerce? A user has installed WooCommerce currency converter plugin' which shows currency converting options anywhere on the store. The user wants to show currency switcher next to 'Add to Cart' button. How to resolve this issue?

Answer

Following given code will help you to display currency converter options n single product page. Add this code to your theme's function.php file.
add_action('woocommerce_single_product_summary','woocs_custom_location',35);
function woocs_custom_location(){
    $plugin_var= "woocommerce-currency-switcher";
    if (in_array( $plugin_var.'/index.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
        echo(do_shortcode('[woocs]'));
    }
}

Question No4

How to change currency position according to currency in WordPress The user has an online store which has two options for currencies i.e. KRW and USD. There are two buttons which switch between these two currencies. When KRW is selected price format: 10,000W and when USD is selected the price format is: 100$. User requirements are to display $100 when USD is selected instead of 100$. User has the following code in function.php
add_filter('woocommerce_price_format','change_currency_pos');

function change_currency_pos( $currency_pos, $currency ) {
     switch( $currency ) {
          case 'USD': $currency_pos = '%1$s%2$s'; break;
          case 'KRW': $currency_pos = '%2$s%1$s'; break;
     }
     return $currency_pos;
}
    function change_currency_pos() {
        $currency_pos = get_option('woocommerece_currency');

        switch($currency_pos) {
            case 'USD': $format = '%1$s%2$s'; break;
            case 'KRW': $format = '%2$s%1$s'; break;
        }
        return $currency_pos;
        }

   add_filter('woocommerce_price_format','change_currency_pos');
None of the above code works, how to resolve this issue?

Answer

Let’s assume that it adds a $_GET variable named currency to the end of URL, but the idea is that you set a value for the woocommerce_currency_pos option based on some data provided by the currency controller plugin.
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
    if( ! isset( $_GET['currency'] ) {
        return false;
    }

    if ( 'USD' == $_GET['currency'] ){
        return 'left';
    } elseif ( 'KRW' == $_GET['currency'] ){
        return 'right';
    } 
}
Again, let’s assume that 'right' is the default position. You only need is the option in case of USD currency. In which case you would need this:
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
    if( isset( $_GET['currency'] && 'USD' == $_GET['currency'] ){
        return 'left';
    } 
}

Question No5

How to show currencies with different numbers of decimal places? The user has an online store developed in WordPress that uses WooCommerce extension and multi currency switcher plugin. Currently, the plugin displays prices in Thai Baht in Thailand and USD as default currency for all other locations. User want to show Thai Baht with 0 decimal places and USD wit h2 decimal places on the shopping cart. is there any function where the user could add that would strip the '.00' from every displayed instance of '00$'? Here' the code the user have
function strip_zeros($baht) {
foreach($baht as $i) {
$baht= strtr($baht, ".00฿", "฿");
}
return $baht;
}
add_filter('the_content','strip_zeros');

Answer

The simple solution is to add the following line of code to the index.php file of your plugin.
public $no_cents = array('THB');

Download WooCommerce Currency Switcher

multicurrency