businessdad icon

WooCommerce - Redirect users based on detected country

businessdad | PRO | 11/21/16 09:28:46 AM UTC | 0 ⭐ | 924 👁️ | Never ⏰ | []
PHP |

1.13 KB

|

None

|

0 👍

/

0 👎

/**
 * Redirects visitors based on their country of origin.
 * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
 *
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
add_action('template_redirect', function() {
  $country = '';
  // Cookie "aelia_customer_country" contains the country detected by the Aelia
  // plugins
  if(!empty($_COOKIE['aelia_customer_country'])) {
    $country = $_COOKIE['aelia_customer_country'];
  }
  
  // Cookie "aelia_billing_country" is a legacy cookie, used by older versions
  // of the plugins
  if(empty($country) && !empty($_COOKIE['aelia_billing_country'])) {
    $country = $_COOKIE['aelia_billing_country'];
  }
  
  // The redirect map will associate each country to the redirect destination
  $redirect_map = array(
    'UK' => 'https://site-for-uk.org',
    'US' => 'https://site-for-us.org',
    'DE' => 'https://site-for-germany.org',
  )
  
  // If we have a destination for a specific country, redirect the user
  if(!empty($redirect_map[$country])) {
    wp_redirect($redirect_map[$country]);
    exit;
  }
});

Comments