Advertisement
businessdad

WooCommerce - Redirect users based on detected country

Nov 21st, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. /**
  2.  * Redirects visitors based on their country of origin.
  3.  * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
  4.  *
  5.  * @author Aelia <support@aelia.co>
  6.  * @link https://aelia.co
  7.  */
  8. add_action('template_redirect', function() {
  9.   $country = '';
  10.   // Cookie "aelia_customer_country" contains the country detected by the Aelia
  11.   // plugins
  12.   if(!empty($_COOKIE['aelia_customer_country'])) {
  13.     $country = $_COOKIE['aelia_customer_country'];
  14.   }
  15.  
  16.   // Cookie "aelia_billing_country" is a legacy cookie, used by older versions
  17.   // of the plugins
  18.   if(empty($country) && !empty($_COOKIE['aelia_billing_country'])) {
  19.     $country = $_COOKIE['aelia_billing_country'];
  20.   }
  21.  
  22.   // The redirect map will associate each country to the redirect destination
  23.   $redirect_map = array(
  24.     'UK' => 'https://site-for-uk.org',
  25.     'US' => 'https://site-for-us.org',
  26.     'DE' => 'https://site-for-germany.org',
  27.   )
  28.  
  29.   // If we have a destination for a specific country, redirect the user
  30.   if(!empty($redirect_map[$country])) {
  31.     wp_redirect($redirect_map[$country]);
  32.     exit;
  33.   }
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement