Advertisement
Guest User

Untitled

a guest
May 29th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // Set currency based on visitor country
  2. if ( ! is_admin() || defined('DOING_AJAX') ) { // to avoid ERROR 500 in WPML admin page
  3. add_filter('woocommerce_init','geo_client_currency', 1);
  4. function geo_client_currency($client_currency) {
  5. global $woocommerce;
  6.  
  7. // If the currency is empty, or the "geolocation cookie" is not set, run the
  8. // geolocation
  9. if(!isset( $_COOKIE['geolocation_currency'])) {
  10. $location = WC_Geolocation::geolocate_ip( '', true, false );
  11.  
  12. $country = isset($location['country']) ? $location['country'] : '';
  13.  
  14. //$country = WC()->customer->get_shipping_country();
  15. switch ($country) {
  16. // EU
  17. case "AT": $client_currency = "EUR"; break;
  18. case "BE": $client_currency = "EUR"; break;
  19. case "CY": $client_currency = "EUR"; break;
  20. case "EE": $client_currency = "EUR"; break;
  21. case "FI": $client_currency = "EUR"; break;
  22. case "FR": $client_currency = "EUR"; break;
  23. case "DE": $client_currency = "EUR"; break;
  24. case "EL": $client_currency = "EUR"; break;
  25. case "IT": $client_currency = "EUR"; break;
  26. case "IE": $client_currency = "EUR"; break;
  27. case "LT": $client_currency = "EUR"; break;
  28. case "LV": $client_currency = "EUR"; break;
  29. case "LU": $client_currency = "EUR"; break;
  30. case "MT": $client_currency = "EUR"; break;
  31. case "NL": $client_currency = "EUR"; break;
  32. case "PT": $client_currency = "EUR"; break;
  33. case "SP": $client_currency = "EUR"; break;
  34. case "SI": $client_currency = "EUR"; break;
  35. case "SK": $client_currency = "EUR"; break;
  36. // DOM-TOM
  37. case "GP": $client_currency = "EUR"; break;
  38. case "GF": $client_currency = "EUR"; break;
  39. case "MQ": $client_currency = "EUR"; break;
  40. case "RE": $client_currency = "EUR"; break;
  41. case "YT": $client_currency = "EUR"; break;
  42. // Others
  43. case "CH": $client_currency = "CHF"; break;
  44. case "GB": $client_currency = "GBP"; break;
  45. case "DK": $client_currency = "DKK"; break;
  46. case "CZ": $client_currency = "CZK"; break;
  47. case "PL": $client_currency = "PLN"; break;
  48. case "CA": $client_currency = "CAD"; break;
  49. default: $client_currency = "USD"; break;
  50. }
  51.  
  52. // Store the currency from geolocation, so that the operation won't be
  53. // performed again
  54. setcookie( 'geolocation_currency', $client_currency, time() + (86400 * 30), "/");
  55. $_COOKIE['geolocation_currency'] = $client_currency;
  56.  
  57. // Force set user cookie when user is not logged in
  58. // @see WCML_Multi_Currency::switch_currency()
  59. global $current_user;
  60.  
  61. if(!empty($woocommerce->session) && empty($woocommerce->session->data)) {
  62. $woocommerce->session->set_customer_session_cookie(true);
  63. }
  64.  
  65. // Store the currency in the WC Session. This will ensure that WCML will
  66. // keep the currency we detected, instead of using the one from the language
  67. if(!empty($woocommerce->session)) {
  68. $woocommerce->session->set('client_currency', $client_currency);
  69. }
  70.  
  71. }
  72.  
  73. return $client_currency;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement