Important notes

The code in this article is considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


How to use the code in the examples

To easiest way to use the code in the examples is to place it in your theme's functions.php. See How to Safely Add Custom Code to WordPress Sites for more information on how to do that. This works in most cases. Please also read the note below, in case you suspect that your snippets are not running.


Tip - What to do if your code doesn't seem to run

If you add the code snippets to the functions.php file, and they don't seem to work, it most likely means that the functions.php file is being loaded too late. In this case, it's better to add the code to a custom mini-plugin. Plugins run much earlier than the theme and its files, and the code they contain will be sure to run, even if the theme is not loaded. 


How to create a mini-plugin as a container for your code snippets

  1. Create a folder in called your-plugin-name on your local computer. You can change "your-plugin-name" to a name of your choice.
  2. Create a file called your-plugin-name.php in the new subfolder.
  3. Add a plugin header to the file: https://developer.wordpress.org/plugins/plugin-basics/header-requirements/. You can just set it to "Plugin Name: YOUR PLUGIN NAME". You can also add other details, if you like, such as author, version and so on.
  4. Add your custom code below the plugin header.
  5. Save the file.
  6. Create a folder called wp-content/plugins/your-plugin-name on your site (e.g. via FTP).
  7. Enter the new folder.
  8. Upload the file you just created.
  9. Go to WordPress > Plugins to enable the custom plugin.


Example 1 - Set the country using a $_GET argument

This example shows how to set the country to a specific value on page load.     

add_action('before_woocommerce_init', function() {
  // Only change the country on the frontend
  if(!is_admin() || defined('DOING_AJAX')) {
    // If a country was selected via a GET argument, pass it through the POST
    // data. This will make it look as if the visitor selected the country using
    // the widget
    if(isset($_GET['aelia_customer_country'])) {
      $_POST['aelia_customer_country'] = $_GET['aelia_customer_country'];
    }
  }
}, 0);

Example 2 - Selecting the country depending on the active language

This example is designed to work with WPML, you might have to adapt it to other multi-language plugins

function aelia_set_country_by_language() {
  // Only change the country on the frontend
  if(!is_admin() || defined('DOING_AJAX')) {
    // Map each language to a country
    $language_country_map = array(
      // English = Ireland
      'EN' => 'IE',
      // German = Germany
      'DE' => 'DE',
      // Swedish = Sweden
      'SV' => 'SE',
      // Italian = Italy
      'IT' => 'IT',
    );
    
    if(defined('ICL_LANGUAGE_CODE') && isset($language_country_map[ICL_LANGUAGE_CODE])) {
      $_POST['aelia_customer_country'] = $language_country_map[ICL_LANGUAGE_CODE];
    }
  }
}
add_action('woocommerce_init', 'aelia_set_country_by_language', 0);

 

Example 3 - Setting the country in specific conditions

If you wish to set the currency only temporarily, or dynamically, for a specific page load, you can write a filter for hook wc_aelia_pbc_customer_country.

function custom_set_country($selected_country) {
  // Replace <SOME CONDITION IS SATISFIED> with the appropriate condition 
  if(<SOME CONDITION IS SATISFIED>) {
    // Force selected country to Ireland
    $selected_country= 'IE';
  }

  return $selected_country;
}
add_filter('wc_aelia_pbc_customer_country', 'custom_set_country', 10);

The above filter will set the country only for current page load. User's cookies and profile will be unaffected, thus the country stored in them will be used on next page load (unless you set it again, via code).


You can purchase the Prices by Country plugin from our online shop.