Advertisement
businessdad

WooCommerce Tax Display by Country - Set country by language

Nov 17th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. /**
  2.  * Sets customer's currency depending on the language, unless a currency was
  3.  * already selected.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Simply add the code to the bottom of your theme's functions.php file, and it
  7.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  8.  *
  9.  * GPL DISCLAIMER
  10.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  11.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  12.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  13.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  14.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  15.  *
  16.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  17.  *
  18.  * @return string
  19.  */
  20. function maybe_set_country_by_language() {
  21.   // Only change the country on the frontend, and only if there isn't already
  22.   // a selected country
  23.   if(empty($_COOKIE['aelia_customer_country']) && !is_admin() || defined('DOING_AJAX')) {
  24.     // Map each language to country
  25.     $country_currency_map = array(
  26.       // Danish = Denmark
  27.       'dk' => 'DK',
  28.       // Norwegian = Norway
  29.       'no' => 'NO',
  30.     );
  31.    
  32.     if(defined('ICL_LANGUAGE_CODE') && isset($language_currency_map[ICL_LANGUAGE_CODE])) {
  33.       $_POST['aelia_customer_country'] = $country_currency_map[ICL_LANGUAGE_CODE];
  34.     }
  35.     else {
  36.       // Default: Denmark (can set any country you like)
  37.       $_POST['aelia_customer_country'] = 'DK';
  38.     }  
  39.   }
  40. }
  41. add_action('before_woocommerce_init', 'maybe_set_country_by_language', 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement