2015-07-10 05:13:30 +00:00
|
|
|
/*global wc_geolocation_params */
|
2014-03-19 23:02:54 +00:00
|
|
|
jQuery( function( $ ) {
|
2021-02-19 22:50:59 +00:00
|
|
|
/**
|
|
|
|
* Contains the current geo hash (or false if the hash
|
|
|
|
* is not set/cannot be determined).
|
|
|
|
*
|
|
|
|
* @type {boolean|string}
|
|
|
|
*/
|
|
|
|
var geo_hash = false;
|
2015-06-17 15:56:06 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
/**
|
|
|
|
* Obtains the current geo hash from the `woocommerce_geo_hash` cookie, if set.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function get_geo_hash() {
|
|
|
|
var geo_hash_cookie = Cookies.get( 'woocommerce_geo_hash' );
|
2015-06-17 15:56:06 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
if ( 'string' === typeof geo_hash_cookie && geo_hash_cookie.length ) {
|
|
|
|
geo_hash = geo_hash_cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we have an active geo hash value but it does not match the `?v=` query var in
|
|
|
|
* current page URL, that indicates that we need to refresh the page.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function needs_refresh() {
|
2021-02-25 21:46:55 +00:00
|
|
|
return geo_hash && ( new URLSearchParams( window.location.search ) ).get( 'v' ) !== geo_hash;
|
2021-02-19 22:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends (or replaces) the geo hash used for links on the current page.
|
|
|
|
*/
|
2015-06-22 13:55:15 +00:00
|
|
|
var $append_hashes = function() {
|
2021-02-19 22:50:59 +00:00
|
|
|
if ( ! geo_hash ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-11 13:31:54 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
$( 'a[href^="' + wc_geolocation_params.home_url + '"]:not(a[href*="v="]), a[href^="/"]:not(a[href*="v="])' ).each( function() {
|
|
|
|
var $this = $( this ),
|
|
|
|
href = $this.attr( 'href' ),
|
|
|
|
href_parts = href.split( '#' );
|
2017-12-11 13:31:54 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
href = href_parts[0];
|
|
|
|
|
|
|
|
if ( href.indexOf( '?' ) > 0 ) {
|
|
|
|
href = href + '&v=' + geo_hash;
|
|
|
|
} else {
|
|
|
|
href = href + '?v=' + geo_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof href_parts[1] !== 'undefined' && href_parts[1] !== null ) {
|
|
|
|
href = href + '#' + href_parts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.attr( 'href', href );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var $geolocate_customer = {
|
|
|
|
url: wc_geolocation_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_customer_location' ),
|
|
|
|
type: 'GET',
|
|
|
|
success: function( response ) {
|
|
|
|
if ( response.success && response.data.hash && response.data.hash !== geo_hash ) {
|
|
|
|
$geolocation_redirect( response.data.hash );
|
|
|
|
}
|
2015-06-17 11:12:49 +00:00
|
|
|
}
|
2015-07-10 05:13:30 +00:00
|
|
|
};
|
2015-06-17 11:12:49 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
/**
|
|
|
|
* Once we have a new hash, we redirect so a new version of the current page
|
|
|
|
* (with correct pricing for the current region, etc) is displayed.
|
|
|
|
*
|
|
|
|
* @param {string} hash
|
|
|
|
*/
|
2015-06-22 13:55:15 +00:00
|
|
|
var $geolocation_redirect = function( hash ) {
|
2021-02-19 22:50:59 +00:00
|
|
|
// Updates our (cookie-based) cache of the hash value. Expires in 1 hour.
|
|
|
|
Cookies.set( 'woocommerce_geo_hash', hash, { expires: 1 / 24 } );
|
|
|
|
|
|
|
|
var this_page = window.location.toString();
|
|
|
|
|
2015-06-22 13:55:15 +00:00
|
|
|
if ( this_page.indexOf( '?v=' ) > 0 || this_page.indexOf( '&v=' ) > 0 ) {
|
2015-07-10 05:13:30 +00:00
|
|
|
this_page = this_page.replace( /v=[^&]+/, 'v=' + hash );
|
2015-06-22 13:55:15 +00:00
|
|
|
} else if ( this_page.indexOf( '?' ) > 0 ) {
|
|
|
|
this_page = this_page + '&v=' + hash;
|
2015-06-17 15:56:06 +00:00
|
|
|
} else {
|
2015-06-22 13:55:15 +00:00
|
|
|
this_page = this_page + '?v=' + hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location = this_page;
|
2015-07-10 05:13:30 +00:00
|
|
|
};
|
2015-06-22 13:55:15 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
/**
|
|
|
|
* Updates any forms on the page so they use the current geo hash.
|
|
|
|
*/
|
|
|
|
function update_forms() {
|
|
|
|
if ( ! geo_hash ) {
|
|
|
|
return;
|
2015-06-17 11:12:49 +00:00
|
|
|
}
|
2015-06-22 13:55:15 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
$( 'form' ).each( function () {
|
|
|
|
var $this = $( this );
|
2015-09-07 10:11:55 +00:00
|
|
|
var method = $this.attr( 'method' );
|
2021-02-19 22:50:59 +00:00
|
|
|
var hasField = $this.find( 'input[name="v"]' ).length > 0;
|
2015-09-02 01:10:00 +00:00
|
|
|
|
2021-02-25 21:46:46 +00:00
|
|
|
if ( method && 'get' === method.toLowerCase() && ! hasField ) {
|
2021-02-19 22:50:59 +00:00
|
|
|
$this.append( '<input type="hidden" name="v" value="' + geo_hash + '" />' );
|
2015-09-01 12:17:59 +00:00
|
|
|
} else {
|
2015-09-01 15:49:03 +00:00
|
|
|
var href = $this.attr( 'action' );
|
|
|
|
if ( href ) {
|
|
|
|
if ( href.indexOf( '?' ) > 0 ) {
|
2021-02-19 22:50:59 +00:00
|
|
|
$this.attr( 'action', href + '&v=' + geo_hash );
|
2015-09-01 15:49:03 +00:00
|
|
|
} else {
|
2021-02-19 22:50:59 +00:00
|
|
|
$this.attr( 'action', href + '?v=' + geo_hash );
|
2015-09-01 15:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 12:17:59 +00:00
|
|
|
}
|
2015-09-01 15:49:03 +00:00
|
|
|
});
|
2021-02-19 22:50:59 +00:00
|
|
|
}
|
2015-09-07 15:58:05 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
// Get the current geo hash. If it doesn't exist, or if it doesn't match the current
|
|
|
|
// page URL, perform a geolocation request.
|
|
|
|
if ( ! get_geo_hash() || needs_refresh() ) {
|
|
|
|
$.ajax( $geolocate_customer );
|
2015-09-01 15:49:03 +00:00
|
|
|
}
|
2015-09-01 12:17:59 +00:00
|
|
|
|
2021-02-19 22:50:59 +00:00
|
|
|
// Page updates.
|
|
|
|
update_forms();
|
|
|
|
$append_hashes();
|
|
|
|
|
2015-06-17 12:59:04 +00:00
|
|
|
$( document.body ).on( 'added_to_cart', function() {
|
2015-06-22 13:55:15 +00:00
|
|
|
$append_hashes();
|
2015-06-17 11:12:49 +00:00
|
|
|
});
|
2021-02-19 22:50:59 +00:00
|
|
|
|
2019-09-23 15:04:31 +00:00
|
|
|
// Enable user to trigger manual append hashes on AJAX operations
|
|
|
|
$( document.body ).on( 'woocommerce_append_geo_hashes', function() {
|
|
|
|
$append_hashes();
|
|
|
|
});
|
2015-07-10 05:13:30 +00:00
|
|
|
});
|