NikolayBezay

Negev prepare cart total product count and price GTM teg.

May 26th, 2021 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.64 KB | None | 0 0
  1. <script>
  2. (function() {
  3.     function updateCartStatus() {
  4.         try {
  5.             var URL = window.location.origin + '/cart/';
  6.             var parser = new DOMParser();
  7.             var doc =  '';
  8.             var data = null;
  9.             var xhr = new XMLHttpRequest();
  10.             xhr.withCredentials = true;
  11.             xhr.addEventListener('readystatechange', function () {
  12.                 if (this.readyState === 4) {
  13.  
  14.                     doc = parser.parseFromString(this.responseText, 'text/html');      
  15.                    
  16.                     var productItemCounter = 0;
  17.                     var productQuantityBlocks = doc.querySelectorAll('.shopping-cart_item .product-quantity');
  18.                     for(var i = 0; i < productQuantityBlocks.length; i++ ) {
  19.                         var pageQuantityInput = productQuantityBlocks[i].querySelector('.shopping-cart-item-quantity');
  20.                        
  21.                         var pageQuantityInputDevider = productQuantityBlocks[i].querySelector('.product-quantity div[unit-data]');
  22.                         if (pageQuantityInput && pageQuantityInputDevider) {
  23.                             pageQuantityInput = parseFloat(pageQuantityInput.value);
  24.                             pageQuantityInputDevider = parseFloat(pageQuantityInputDevider.getAttribute('unit-data'));
  25.  
  26.                             productItemCounter += pageQuantityInput / pageQuantityInputDevider;
  27.                         }
  28.                     }
  29.                    var cartTotalPrice = doc.querySelector('.total-cart-display-price');
  30.                    if (cartTotalPrice) {
  31.                        cartTotalPrice = parseFloat(cartTotalPrice.textContent.replaceAll(',', ''));
  32.                    } else {
  33.                        cartTotalPrice = 0;
  34.                    }
  35.                    window.localStorage.setItem('cartTotal_ProductCount', productItemCounter);
  36.                    window.localStorage.setItem('cartTotal_ProductPrice', cartTotalPrice);
  37.                     var event = document.createEvent('Event');
  38.                    event.initEvent('adoricTotalCartUpdated', true, true);
  39.                     document.querySelector('body').dispatchEvent(event);
  40.                }
  41.            });
  42.            xhr.open('GET', URL);
  43.            xhr.setRequestHeader('cache-control', 'no-cache');
  44.            xhr.send(data);
  45.        } catch(error) {
  46.            console.log(error.message);
  47.        }
  48.    }
  49.  
  50.    document.querySelector('body').addEventListener('click', function(event) {
  51.        if (event.target.matches('.addToCart')) {
  52.            setTimeout(updateCartStatus, 500);
  53.        }
  54.    });
  55.    document.querySelector('body').addEventListener('touchend', function(event) {
  56.        if (event.target.matches('.addToCart')) {
  57.            setTimeout(updateCartStatus, 500);
  58.        }
  59.    });
  60. })();
  61. </script>
  62.  
Add Comment
Please, Sign In to add comment