Advertisement
NikolayBezay

Loccitane GTM teg to add prize product after 300 in total price(UPDATED 2)

Apr 13th, 2022 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. <script>
  2. (function() {
  3. var timeToLaunch = 3000;
  4. var productId = 12703;
  5. var prizeProductID = 14530;
  6. var PRIZE_PRODUCT_CART_LIMIT = 300;
  7. var wait = 0;
  8.  
  9. function customerDataReload() {
  10. require([
  11. 'Magento_Customer/js/customer-data'
  12. ], function(customerData) {
  13. var sections = ['cart'];
  14. customerData.invalidate(sections);
  15. customerData.reload(sections, true);
  16. });
  17. }
  18.  
  19. function isProduct(product) {
  20. return parseInt(product.product_id) === productId;
  21. }
  22.  
  23. function isPrizeProduct(product) {
  24. return parseInt(product.product_id) === prizeProductID;
  25. }
  26.  
  27. function checkProductAndPrice() {
  28. var cartData = JSON.parse(window.localStorage.getItem('mage-cache-storage'));
  29.  
  30. if (wait < 100 && (!cartData || !cartData.cart)) {
  31. wait++;
  32. setTimeout(checkProductAndPrice, 200);
  33. return;
  34. }
  35.  
  36. if (cartData && cartData.cart && cartData.cart.subtotalWithDiscount && cartData.cart.items && cartData.cart.summary_count) {
  37. var summaryCount = cartData.cart.summary_count;
  38. var productItems = cartData.cart.items;
  39. var checkbox = document.querySelector('#loyalty-birthday');
  40. var currentTotalPrice = parseFloat(cartData.cart.subtotalWithDiscount).toFixed(0);
  41.  
  42. if (parseInt(currentTotalPrice) >= PRIZE_PRODUCT_CART_LIMIT && !productItems.some(isPrizeProduct)) {
  43. window.adoric && window.adoric.trigger('showAddPrizeProductToTheCart');
  44. }
  45. if ((parseInt(currentTotalPrice) < PRIZE_PRODUCT_CART_LIMIT && productItems.some(isPrizeProduct)) || (checkbox && checkbox.checked)) {
  46. window.adoric && window.adoric.trigger('showRemovePrizeProductToTheCart');
  47. }
  48.  
  49. if (parseInt(currentTotalPrice) >= 300 && !productItems.some(isProduct) && (!checkbox || (checkbox && !(checkbox.checked)))) {
  50. window.adoric && window.adoric.trigger('addAllInOneProduct');
  51. return;
  52. }
  53. if ((parseInt(currentTotalPrice) < 300 && productItems.some(isProduct)) || (checkbox && checkbox.checked)) {
  54. window.adoric && window.adoric.trigger('delAllInOneProduct');
  55. return;
  56. }
  57. }
  58. }
  59.  
  60. function countProductsCheck(event) {
  61. var carouselBtn = event.target.closest('.dai-carousel-btn');
  62. var homeBtn = event.target.closest('.tocart');
  63. var moreBtn = event.target.closest('.qty-more');
  64. var lessBtn = event.target.closest('.qty-less');
  65. var actionPrimary = event.target.closest('.action-primary');
  66.  
  67. if (homeBtn || carouselBtn || moreBtn || lessBtn || actionPrimary) {
  68. setTimeout(function() {
  69. checkProductAndPrice();
  70. }, timeToLaunch);
  71. }
  72. }
  73.  
  74. function updateByCheckboxToggle() {
  75. var checkbox = document.querySelector('#loyalty-birthday');
  76. checkbox && checkbox.addEventListener('change', checkProductAndPrice);
  77. }
  78.  
  79. function stateCheck(callback) {
  80. if (document.readyState === 'complete') {
  81. callback();
  82. } else {
  83. setTimeout(stateCheck, 500, callback);
  84. }
  85. }
  86.  
  87. if (document.readyState !== 'complete') {
  88. stateCheck(function() {
  89. setTimeout(function() {
  90. customerDataReload();
  91. updateByCheckboxToggle();
  92. document.querySelector('body').addEventListener("click", countProductsCheck);
  93. document.querySelector('body').addEventListener("touchend", countProductsCheck);
  94. checkProductAndPrice();
  95. }, timeToLaunch);
  96. });
  97. } else {
  98. setTimeout(function() {
  99. customerDataReload();
  100. updateByCheckboxToggle();
  101. document.querySelector('body').addEventListener("click", countProductsCheck);
  102. document.querySelector('body').addEventListener("touchend", countProductsCheck);
  103. checkProductAndPrice();
  104. }, timeToLaunch);
  105. }
  106.  
  107. })();
  108. </script>
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement