Home Forums Pre-Sale Switching currency in Woo Multi Currency when changing country in another plugin

Switching currency in Woo Multi Currency when changing country in another plugin

  • Author
    Posts
  • #74131

    Juanjo Guirao
    Participant

    Hi,
    I am planning to use WooCommerce Multi Currency in combination with another plugin: WooCommerce Country Restrictions – Advanced Pro.

    WooCommerce Country Restrictions – Advanced Pro allows visitors to select a country, and will display a different set of products for each country. Howeverm I also want the currency displayed to change to the appropriate one when the visitors select a new country, but I am aware this will not happen automatically out-of-the-box because WooCommerce Multi Currency and WooCommerce Country Restrictions – Advanced Pro do not communicate with each other.

    I will have to add a PHP code snippet to automatically trigger a currency switch in WooCommerce Multi Currency when the country is changed in WooCommerce Country Restrictions – Advanced Pro.

    I have spoken with the support of WooCommerce Country Restrictions – Advanced Pro and they have provided the following PHP code snippet, which needs to be customized to work properly with the specific multicurrency plugin used:

    ———————–

    add_filter(‘wcacr_country_selector/link_url’, function($url, $country_flag) {
    // Country code => currency code
    $currencies = array(
    ‘KR’ => ‘KRW’,
    ‘MY’ => ‘MYR’,
    ‘SG’ => ‘SGD’,
    ‘BN’ => ‘BND’,
    ‘TW’ => ‘USD’,
    ‘CA’ => ‘CAD’,
    ‘US’ => ‘USD’,
    );
    $current_country_code = $country_flag[‘abbreviation’];

    // if we have a currency for the country, add the parameter wccr_currency=xx to the URL
    // so the currency switcher changes the currency automatically
    if( isset($currencies[ $current_country_code ])){
    $currency = $currencies[ $current_country_code ];
    $url = add_query_arg(‘wccr_currency’, $currency, esc_html($url));
    }

    return $url;
    }, 10, 2);

    ———————–

    My questions to you:

    1. Will this approach work? Do you know if anyone has done this before? Is it the best approach?

    2. Can you guide me in customizing this code snippet to work with WooCommerce Multi Currency? Will you help me get it to work if I purchase WooCommerce Multi Currency?

    Thank you.

    Kind regards
    Juan

You must be logged in to reply to this topic. Click here to login or register