skip to Main Content

I am using the plugin FOX – Currency Switcher Professional for WooCommerce (former name is WOOCS) and everything was working smoothly. Then I have installed a budgeter plugin. Unfortunately, it is not multicurrency and not compatible with the Currency Switcher.

Therefore, I had to create two instances of the budgeter: one for Argentine pesos and another for dollars. These two instances are loaded on the same page,"/prices/", and from the Divi Builder create two code modules that display according to a conditional: if the "ARS" parameter of currency exists in the URL, display the module that has the shortcode of the budget in pesos; instead, if there is "USD" as a currency parameter in the URL, then hide the other one and load the budgeter in dollars.

In the WOOCS module I have geolocation activated, and it takes ARS only for Argentina, for the rest of the countries it takes dollars.

But sometimes works and sometimes not. Recently, I have checked it three times: Argentina (worked), EU (worked), Argentina again (worked), Brasil (didn’t work). They were all consecutive tests: Changing the location on the VPN, checking the IP at whatismyipaddress.com and then opening a new incognito window.

I’ve not caching plugins enabled.

This is the code I have installed on my site to add the currency parameter:

function agregar_moneda_a_url() {
    global $WOOCS;
    
    $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $parsed_url = parse_url($current_url);

    if (isset($parsed_url['query']) && !empty($parsed_url['query'])) {
        $params = [];
        parse_str($parsed_url['query'], $params);
        
        if (isset($params['currency'])) {
            return; // El parámetro"currency" ya está presente en la URL, no se hace nada
        }
    }
    if (is_page('precios')) {
        $moneda = $WOOCS->current_currency;
        $new_url = add_query_arg('currency', $moneda, $current_url);
        wp_redirect($new_url);
        exit;
    }
}
add_action('template_redirect', 'agregar_moneda_a_url');

As you can see, when it doesn’t work, the URL takes the value of currency configured as initial in the plugin options, not the one it should according to the geolocation. However, the currency exchanger is taking the value of currency according to the geolocation.

I understand that I am close to the solution, but I must be loading the code at the wrong time, right? If it loaded the code at the same time or after the multi-currency plugin takes the geolocation value, then it should take the correct value from WOOCS variable.

If you could guide me with this, I would really appreciate it.

2

Answers


  1. It seems like you are facing an issue with the currency switching functionality on your WooCommerce website, specifically when it comes to geolocation and the custom budgeter plugin. Let’s try to troubleshoot the problem.

    First, make sure that the code snippet you provided is correctly added to your site. It appears to be a WordPress hook that triggers the addition of the currency parameter to the URL on the "precios" page. Double-check that the hook is firing correctly and that the function ‘agregar_moneda_a_url’ is being executed as expected.

    Assuming the code snippet is implemented correctly, the issue might be related to the timing of when the geolocation and currency switching functions are being executed. The geolocation feature of the Currency Switcher plugin determines the user’s location and selects the appropriate currency based on that information.

    However, if the custom budgeter plugin is loading before the geolocation process occurs, it might not be aware of the currency selected by the Currency Switcher plugin. This could explain why the budgeter plugin is sometimes using the initial currency configuration instead of the geolocation-based currency.

    To address this timing issue, you could try the following steps:

    1. Check if the custom budgeter plugin has any hooks or filters that allow you to manipulate its behavior. Look for any available actions or filters in the plugin’s documentation or source code.

    2. If you find suitable hooks or filters, you can use them to delay the initialization of the custom budgeter plugin until after the Currency Switcher plugin has completed its geolocation process. This way, the budgeter plugin will be aware of the selected currency.
      Modify your code snippet to only redirect the user to the new URL if the Currency Switcher plugin has already determined the currency based on geolocation. You can do this by checking if the ‘$WOOCS->current_currency’ variable has been set before redirecting. Otherwise, allow the budgeter plugin to use its default currency.

    3. Here’s an updated version of the code snippet incorporating the above changes:

              function agregar_moneda_a_url() {
               global $WOOCS;
      
               $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
               $parsed_url = parse_url($current_url);
      
               if (isset($parsed_url['query']) && !empty($parsed_url['query'])) {
                   $params = [];
                   parse_str($parsed_url['query'], $params);
      
                   if (isset($params['currency'])) {
                       return; // The "currency" parameter is already present in the URL, do nothing
                   }
               }
      
               if (is_page('precios') && isset($WOOCS->current_currency)) {
                   $moneda = $WOOCS->current_currency;
                   $new_url = add_query_arg('currency', $moneda, $current_url);
                   wp_redirect($new_url);
                   exit;
               }
           }
           add_action('template_redirect', 'agregar_moneda_a_url');
      

    By adding the additional check for ‘$WOOCS->current_currency’, the code will only redirect if the Currency Switcher plugin has already determined the currency based on geolocation.

    Please note that this solution assumes that the geolocation feature of the Currency Switcher plugin has been properly set up and is working correctly. Make sure to test the website thoroughly after applying these changes to ensure the desired behavior is achieved consistently.

    If the issue persists or you encounter any other problems, I recommend reaching out to the plugin developers for further assistance. They will have a better understanding of their product and can provide specific guidance based on your setup.

    Login or Signup to reply.
  2. So as your code doesn’t work by adding the currency as a query string based on $WOOCS global variable (geolocated currency), let’s use the WooCommerce geolocation instead (integrated Maxmind GeoLite2 free for countries).

    So we set the currency query string based on the WooCommerce geolocated country: if the country is Argentina ("AR" as country code) then we set "ARS" as query string value, if not we set "USD" as query string value.

    You will need to check that you have set, in WooCommerce > Settings > Integration, the MaxMind License Key. And in WooCommerce > Settings > General in "Default customer location" use "Geolocate" option.

    My code attempt:

    add_action('template_redirect', 'agregar_moneda_a_url');
    function agregar_moneda_a_url() {
        global $WOOCS;
        
        $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $parsed_url  = parse_url($current_url);
    
        if (isset($parsed_url['query']) && ! empty($parsed_url['query'])) { 
            $params = [];
            parse_str($parsed_url['query'], $params);
    
            if (isset($params['currency'])) {
                return; // El parámetro"currency" ya está presente en la URL, no se hace nada
            }
        }
        
        if ( ! is_page('precios') ) {
            // Get an instance of the WC_Geolocation object class
            $geo_instance  = new WC_Geolocation();
            // Get geolocated user geo data.
            $user_country  = $geo_instance->geolocate_ip()['country'];
            // We set the currency code depending on the user geolocated country
            $currency = $user_country === 'AR' ? 'ARS' : 'USD';
            // Redirection with currency query string
            wp_safe_redirect( add_query_arg('currency', $currency, $current_url) );
            exit;
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It could work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search