Skip to content

Commit

Permalink
Added - helper function for visitor Geo info
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Igna committed Oct 23, 2018
1 parent 0ea8e13 commit 02323bc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,40 @@ function mf() {
}

}



// Get visitor location - Country

if (!function_exists('getVisitorCountryCode')) {
function getVisitorCountryCode(): string {
return strtoupper(apply_filters('visitor_country_code', ''));
}
}

add_filter('user_country_code', 'layered_visitor_country_code_woocommerce');
add_filter('user_country_code', 'layered_visitor_country_code_headers');

function layered_visitor_country_code_woocommerce(string $countryCode = ''): string {
if (empty($countryCode) && class_exists('WC_Geolocation')) {
$location = WC_Geolocation::geolocate_ip();
if ($location['country'] && !in_array($location['country'], array('A1', 'A2', 'EU', 'AP'))) {
$countryCode = $location['country'];
}
}

return $countryCode;
}

function if_menu_user_country_code_headers(string $countryCode = ''): string {
if (empty($countryCode)) {
foreach (['HTTP_CF_IPCOUNTRY', 'X-AppEngine-country', 'CloudFront-Viewer-Country', 'GEOIP_COUNTRY_CODE', 'HTTP_X_COUNTRY_CODE', 'HTTP_X_GEO_COUNTRY'] as $key) {
if (isset($_SERVER[$key]) && $_SERVER[$key] && !in_array($_SERVER[$key], ['XX', 'ZZ', 'A1', 'A2', 'EU', 'AP'])) {
return $_SERVER[$key];
}
}
}

return $countryCode;
}

0 comments on commit 02323bc

Please sign in to comment.