businessdad icon

WooCommerce Tax Display by Country - Set country by language

businessdad | PRO | 11/17/17 10:49:11 AM UTC | 0 ⭐ | 956 👁️ | Never ⏰ | []
PHP |

1.86 KB

|

None

|

0 👍

/

0 👎

/**
 * Sets customer's currency depending on the language, unless a currency was
 * already selected.
 *
 * HOW TO USE THIS CODE
 * Simply add the code to the bottom of your theme's functions.php file, and it
 * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
 *
 * GPL DISCLAIMER
 * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 *
 * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
 *
 * @return string
 */
function maybe_set_country_by_language() {
  // Only change the country on the frontend, and only if there isn't already
  // a selected country
  if(empty($_COOKIE['aelia_customer_country']) && !is_admin() || defined('DOING_AJAX')) {
    // Map each language to country
    $country_currency_map = array(
      // Danish = Denmark
      'dk' => 'DK',
      // Norwegian = Norway
      'no' => 'NO',
    );
    
    if(defined('ICL_LANGUAGE_CODE') && isset($language_currency_map[ICL_LANGUAGE_CODE])) {
      $_POST['aelia_customer_country'] = $country_currency_map[ICL_LANGUAGE_CODE];
    }
    else {
      // Default: Denmark (can set any country you like)
      $_POST['aelia_customer_country'] = 'DK';
    }  
  }
}
add_action('before_woocommerce_init', 'maybe_set_country_by_language', 0);

Comments