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.
Setting the country
The Tax Display 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"] and $POST ["aelia_billing_country"] (this is a legacy parameter, but it's worth setting it anyway, for backward compatibility) in a hook attached to woocommerce_init event. The hook must have a priority of 0 (zero), as the Tax Display 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'] = strtoupper($_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 for bots
Out of the box, search engine crawlers are treated like any other visitor. If you need to make sure that crawlers view prices for a specific country, the code below shows how you can do that.
function aelia_visitor_is_bot() {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
$bot_types = 'bot|crawl|slurp|spider';
return !empty($user_agent) ? preg_match("/$bot_types/", $user_agent) > 0 : false;
}
function set_country_for_bots() {
// Only change the country for bots, and only on the frontend
if(aelia_visitor_is_bot() && (!is_admin() || defined('DOING_AJAX'))) {
// Always force selected country to US. Bots will always see prices for that
// country
$_POST['aelia_customer_country'] = 'US';
}
}
add_action('woocommerce_init', 'set_country_for_bots', 0);You can purchase the Tax Display by Country plugin from our online shop.