Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Set currency based on visitor country
- if ( ! is_admin() || defined('DOING_AJAX') ) { // to avoid ERROR 500 in WPML admin page
- add_filter('woocommerce_init','geo_client_currency', 1);
- function geo_client_currency($client_currency) {
- global $woocommerce;
- // If the currency is empty, or the "geolocation cookie" is not set, run the
- // geolocation
- if(!isset( $_COOKIE['geolocation_currency'])) {
- $location = WC_Geolocation::geolocate_ip( '', true, false );
- $country = isset($location['country']) ? $location['country'] : '';
- //$country = WC()->customer->get_shipping_country();
- switch ($country) {
- // EU
- case "AT": $client_currency = "EUR"; break;
- case "BE": $client_currency = "EUR"; break;
- case "CY": $client_currency = "EUR"; break;
- case "EE": $client_currency = "EUR"; break;
- case "FI": $client_currency = "EUR"; break;
- case "FR": $client_currency = "EUR"; break;
- case "DE": $client_currency = "EUR"; break;
- case "EL": $client_currency = "EUR"; break;
- case "IT": $client_currency = "EUR"; break;
- case "IE": $client_currency = "EUR"; break;
- case "LT": $client_currency = "EUR"; break;
- case "LV": $client_currency = "EUR"; break;
- case "LU": $client_currency = "EUR"; break;
- case "MT": $client_currency = "EUR"; break;
- case "NL": $client_currency = "EUR"; break;
- case "PT": $client_currency = "EUR"; break;
- case "SP": $client_currency = "EUR"; break;
- case "SI": $client_currency = "EUR"; break;
- case "SK": $client_currency = "EUR"; break;
- // DOM-TOM
- case "GP": $client_currency = "EUR"; break;
- case "GF": $client_currency = "EUR"; break;
- case "MQ": $client_currency = "EUR"; break;
- case "RE": $client_currency = "EUR"; break;
- case "YT": $client_currency = "EUR"; break;
- // Others
- case "CH": $client_currency = "CHF"; break;
- case "GB": $client_currency = "GBP"; break;
- case "DK": $client_currency = "DKK"; break;
- case "CZ": $client_currency = "CZK"; break;
- case "PL": $client_currency = "PLN"; break;
- case "CA": $client_currency = "CAD"; break;
- default: $client_currency = "USD"; break;
- }
- // Store the currency from geolocation, so that the operation won't be
- // performed again
- setcookie( 'geolocation_currency', $client_currency, time() + (86400 * 30), "/");
- $_COOKIE['geolocation_currency'] = $client_currency;
- // Force set user cookie when user is not logged in
- // @see WCML_Multi_Currency::switch_currency()
- global $current_user;
- if(!empty($woocommerce->session) && empty($woocommerce->session->data)) {
- $woocommerce->session->set_customer_session_cookie(true);
- }
- // Store the currency in the WC Session. This will ensure that WCML will
- // keep the currency we detected, instead of using the one from the language
- if(!empty($woocommerce->session)) {
- $woocommerce->session->set('client_currency', $client_currency);
- }
- }
- return $client_currency;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement