NikolayBezay

Sodastream dynamic embed to add functionality "add to cart" for campaigns with recommendation element.

Nov 27th, 2020 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.56 KB | None | 0 0
  1.  <script>
  2.      (function() {
  3.          function getUrlParameter(name, URL) {
  4.              name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  5.              var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
  6.              var results = regex.exec(URL);
  7.              return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, " "));
  8.          }
  9.  
  10.          function adoricAddToCart(productId) {
  11.              var data = {
  12.                  "id": productId,
  13.                  "quantity": 1
  14.              };
  15.              var xhr = new XMLHttpRequest();
  16.              xhr.withCredentials = true;
  17.              xhr.addEventListener('readystatechange', function() {
  18.                  if (this.readyState === 4) {
  19.                      console.log(this.responseText);
  20.                      window.location.reload();
  21.                  }
  22.              });
  23.              xhr.open('POST', window.location.origin + '/cart/add.js');
  24.              xhr.setRequestHeader('content-type', 'application/json');
  25.              xhr.setRequestHeader('cache-control', 'no-cache');
  26.              xhr.send(JSON.stringify(data));
  27.          }
  28.          if (window.Element && !Element.prototype.closest) {
  29.             Element.prototype.closest = function(s) {
  30.                 var matches = (this.document || this.ownerDocument).querySelectorAll(s),
  31.                     i, el = this;
  32.                  do {
  33.                      i = matches.length;
  34.                      while (--i >= 0 && matches.item(i) !== el) {};
  35.                  } while ((i < 0) && (el = el.parentElement));
  36.                 return el;
  37.             };
  38.         }
  39.         var currentCampaignRepeater = document.querySelector('.' + self.identifier + ' .repeater-content');
  40.         currentCampaignRepeater.addEventListener('click', function(event) {
  41.             var productItem = event.target.closest('.repeater-item');
  42.             event.preventDefault();
  43.             if (event.target.getAttribute('role') === 'button' && productItem) {
  44.                 var productInformation = JSON.parse(productItem.dataset.productInfo);
  45.                 var productId = getUrlParameter('variant', productInformation.link);
  46.                 window.ga && window.ga('t0.send', 'event', {
  47.                     eventCategory: "Adoric - " + self.campaignTitle + '(' + self.versionTitle + ')',
  48.                     eventAction: 'click',
  49.                     eventLabel: 'Added to the cart - ' + productInformation.title,
  50.                 });
  51.                 adoricAddToCart(productId);
  52.             }
  53.         });
  54.     })();
  55. </script>
Add Comment
Please, Sign In to add comment