Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- (function() {
- function updateCartStatus() {
- try {
- var URL = window.location.origin + '/cart/';
- var parser = new DOMParser();
- var doc = '';
- var data = null;
- var xhr = new XMLHttpRequest();
- xhr.withCredentials = true;
- xhr.addEventListener('readystatechange', function () {
- if (this.readyState === 4) {
- doc = parser.parseFromString(this.responseText, 'text/html');
- var productItemCounter = 0;
- var productQuantityBlocks = doc.querySelectorAll('.shopping-cart_item .product-quantity');
- for(var i = 0; i < productQuantityBlocks.length; i++ ) {
- var pageQuantityInput = productQuantityBlocks[i].querySelector('.shopping-cart-item-quantity');
- var pageQuantityInputDevider = productQuantityBlocks[i].querySelector('.product-quantity div[unit-data]');
- if (pageQuantityInput && pageQuantityInputDevider) {
- pageQuantityInput = parseFloat(pageQuantityInput.value);
- pageQuantityInputDevider = parseFloat(pageQuantityInputDevider.getAttribute('unit-data'));
- productItemCounter += pageQuantityInput / pageQuantityInputDevider;
- }
- }
- var cartTotalPrice = doc.querySelector('.total-cart-display-price');
- if (cartTotalPrice) {
- cartTotalPrice = parseFloat(cartTotalPrice.textContent.replaceAll(',', ''));
- } else {
- cartTotalPrice = 0;
- }
- window.localStorage.setItem('cartTotal_ProductCount', productItemCounter);
- window.localStorage.setItem('cartTotal_ProductPrice', cartTotalPrice);
- var event = document.createEvent('Event');
- event.initEvent('adoricTotalCartUpdated', true, true);
- document.querySelector('body').dispatchEvent(event);
- }
- });
- xhr.open('GET', URL);
- xhr.setRequestHeader('cache-control', 'no-cache');
- xhr.send(data);
- } catch(error) {
- console.log(error.message);
- }
- }
- document.querySelector('body').addEventListener('click', function(event) {
- if (event.target.matches('.addToCart')) {
- setTimeout(updateCartStatus, 500);
- }
- });
- document.querySelector('body').addEventListener('touchend', function(event) {
- if (event.target.matches('.addToCart')) {
- setTimeout(updateCartStatus, 500);
- }
- });
- })();
- </script>
Add Comment
Please, Sign In to add comment