Aucune description

geolocation.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*global wc_geolocation_params */
  2. jQuery( function( $ ) {
  3. /**
  4. * Contains the current geo hash (or false if the hash
  5. * is not set/cannot be determined).
  6. *
  7. * @type {boolean|string}
  8. */
  9. var geo_hash = false;
  10. /**
  11. * Obtains the current geo hash from the `woocommerce_geo_hash` cookie, if set.
  12. *
  13. * @returns {boolean}
  14. */
  15. function get_geo_hash() {
  16. var geo_hash_cookie = Cookies.get( 'woocommerce_geo_hash' );
  17. if ( 'string' === typeof geo_hash_cookie && geo_hash_cookie.length ) {
  18. geo_hash = geo_hash_cookie;
  19. return true;
  20. }
  21. return false;
  22. }
  23. /**
  24. * If we have an active geo hash value but it does not match the `?v=` query var in
  25. * current page URL, that indicates that we need to refresh the page.
  26. *
  27. * @returns {boolean}
  28. */
  29. function needs_refresh() {
  30. return geo_hash && ( new URLSearchParams( window.location.search ) ).get( 'v' ) !== geo_hash;
  31. }
  32. /**
  33. * Appends (or replaces) the geo hash used for links on the current page.
  34. */
  35. var $append_hashes = function() {
  36. if ( ! geo_hash ) {
  37. return;
  38. }
  39. $( 'a[href^="' + wc_geolocation_params.home_url + '"]:not(a[href*="v="]), a[href^="/"]:not(a[href*="v="])' ).each( function() {
  40. var $this = $( this ),
  41. href = $this.attr( 'href' ),
  42. href_parts = href.split( '#' );
  43. href = href_parts[0];
  44. if ( href.indexOf( '?' ) > 0 ) {
  45. href = href + '&v=' + geo_hash;
  46. } else {
  47. href = href + '?v=' + geo_hash;
  48. }
  49. if ( typeof href_parts[1] !== 'undefined' && href_parts[1] !== null ) {
  50. href = href + '#' + href_parts[1];
  51. }
  52. $this.attr( 'href', href );
  53. });
  54. };
  55. var $geolocate_customer = {
  56. url: wc_geolocation_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_customer_location' ),
  57. type: 'GET',
  58. success: function( response ) {
  59. if ( response.success && response.data.hash && response.data.hash !== geo_hash ) {
  60. $geolocation_redirect( response.data.hash );
  61. }
  62. }
  63. };
  64. /**
  65. * Once we have a new hash, we redirect so a new version of the current page
  66. * (with correct pricing for the current region, etc) is displayed.
  67. *
  68. * @param {string} hash
  69. */
  70. var $geolocation_redirect = function( hash ) {
  71. // Updates our (cookie-based) cache of the hash value. Expires in 1 hour.
  72. Cookies.set( 'woocommerce_geo_hash', hash, { expires: 1 / 24 } );
  73. var this_page = window.location.toString();
  74. if ( this_page.indexOf( '?v=' ) > 0 || this_page.indexOf( '&v=' ) > 0 ) {
  75. this_page = this_page.replace( /v=[^&]+/, 'v=' + hash );
  76. } else if ( this_page.indexOf( '?' ) > 0 ) {
  77. this_page = this_page + '&v=' + hash;
  78. } else {
  79. this_page = this_page + '?v=' + hash;
  80. }
  81. window.location = this_page;
  82. };
  83. /**
  84. * Updates any forms on the page so they use the current geo hash.
  85. */
  86. function update_forms() {
  87. if ( ! geo_hash ) {
  88. return;
  89. }
  90. $( 'form' ).each( function () {
  91. var $this = $( this );
  92. var method = $this.attr( 'method' );
  93. var hasField = $this.find( 'input[name="v"]' ).length > 0;
  94. if ( method && 'get' === method.toLowerCase() && ! hasField ) {
  95. $this.append( '<input type="hidden" name="v" value="' + geo_hash + '" />' );
  96. } else {
  97. var href = $this.attr( 'action' );
  98. if ( href ) {
  99. if ( href.indexOf( '?' ) > 0 ) {
  100. $this.attr( 'action', href + '&v=' + geo_hash );
  101. } else {
  102. $this.attr( 'action', href + '?v=' + geo_hash );
  103. }
  104. }
  105. }
  106. });
  107. }
  108. // Get the current geo hash. If it doesn't exist, or if it doesn't match the current
  109. // page URL, perform a geolocation request.
  110. if ( ! get_geo_hash() || needs_refresh() ) {
  111. $.ajax( $geolocate_customer );
  112. }
  113. // Page updates.
  114. update_forms();
  115. $append_hashes();
  116. $( document.body ).on( 'added_to_cart', function() {
  117. $append_hashes();
  118. });
  119. // Enable user to trigger manual append hashes on AJAX operations
  120. $( document.body ).on( 'woocommerce_append_geo_hashes', function() {
  121. $append_hashes();
  122. });
  123. });