Setting the country 

The Prices by Country plugin will check for a selected country during the woocommerce_init event. To force the selected country to a specific one, set the value of $_POST ["aelia_customer_country"] in a hook attached to woocommerce_init event. The hook must have a priority of 0 (zero), as the Prices by Country plugin

 will run its own with a priority of 1.


Example

To use the code in the examples, simply place it in your theme's functions.php.


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 temporarily

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) {
  // 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.