DidouS

cookie-geo-check.js

Nov 17th, 2020 (edited)
659
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * this helper script will try to determine if the visitor is viewing
  3.  * the shop preferable for his/her region. This should prevent
  4.  * accidental filling of a shopping basket and finding out one can't
  5.  * order when attempting to checkout.
  6.  *
  7.  *
  8.  */
  9.  
  10. //
  11. // /** Add following CSS and assign classname 'bbuilder-hide'  to the node */
  12. //
  13. // html:not(.fl-builder-edit) .bbuilder-hide {
  14. //  opacity:0;
  15. //  /* prevent click event when this class is active */
  16. //  pointer-events: none;
  17.  
  18. // }
  19.  
  20.  
  21. const bobino = {
  22. /**
  23.      * Set a Cookie value with a certain expiration time (in days)
  24.      * @param {[type]} cname  [description]
  25.      * @param {[type]} cvalue [description]
  26.      * @param {[type]} exdays [description]
  27.      */
  28.     setCookie: function (cname, cvalue, exdays ) {
  29.  
  30.         // set a default value for exdays
  31.         if ( typeof exdays == 'undefined' ) var exdays = 365;
  32.  
  33.         var d = new Date();
  34.  
  35.         d.setTime(d.getTime() + ( 1000 * 60 * 60 * 24 * exdays ) );
  36.  
  37.         var expires = "expires="+d.toUTCString();
  38.  
  39.         document.cookie = cname + "=" + cvalue + ";path=/; " + expires;
  40.     },
  41.  
  42.     /**
  43.      * Get the value of a Cookie
  44.      *
  45.      * @param  {[type]} cname [description]
  46.      * @return {[type]}       [description]
  47.      */
  48.     getCookie: function (cname) {
  49.         var name = cname + "=";
  50.         var ca = document.cookie.split(';');
  51.         for(var i=0; i<ca.length; i++) {
  52.             var c = ca[i];
  53.             while (c.charAt(0)==' ') c = c.substring(1);
  54.             if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  55.         }
  56.         return "";
  57.     }
  58.  
  59. };
  60.  
  61. const getGeoIp = () => {
  62.  
  63.     let myPromise = new Promise(
  64.         resolve => {
  65.  
  66.             jQuery.ajax( {
  67.                 url: 'https://freegeoip.app/json/',
  68.                 method : 'GET',
  69.             }).done( function( data ) {
  70.                 resolve(data);
  71.             })          
  72.         }
  73.     );
  74.  
  75.     myPromise.then( ( data ) => {
  76.  
  77.         console.log( data.country_name );
  78.  
  79.         if ( data.country_code !== 'CA' || data.country_code !== 'US' ) {
  80.  
  81.             setTimeout( function() { jQuery( '.bobino-geo-message' ).removeClass( 'bbuilder-hide' ); } , 2000 );
  82.  
  83.         }
  84.  
  85.     }  );
  86.  
  87. }
  88.  
  89.  
  90.  
  91. /**
  92.  * Wait for document ready
  93.  */
  94. (function($){
  95.  
  96.     $( document ).ready( function() {
  97.  
  98.         if ( bobino.getCookie( 'shop-accepted' ) ) return;
  99.         // if cookie not found, do a geo-check to warn the visitor
  100.         getGeoIp();
  101.  
  102.     });
  103.  
  104.     $( '.accept-and-continue a' ).on( 'click' , function() {
  105.         bobino.setCookie( 'shop-accepted' , true , 1 );
  106.         setTimeout( function() { jQuery( '.bobino-geo-message' ).addClass( 'bbuilder-hide' ); } , 200 );
  107.  
  108.     } );
  109.  
  110. })(jQuery);
  111.  
  112.  
RAW Paste Data