From d5b654b4032714574f74c10d18867b1b52b1f61a Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Thu, 30 Nov 2023 23:41:08 +0100 Subject: [PATCH] Fix bug on grouped products when using product ID prefix. --- assets/integration/woocommerce.js | 2 +- src/Integration/WooCommerce.php | 2 +- src/js/woocommerce.js | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/integration/woocommerce.js b/assets/integration/woocommerce.js index 8b18175..8fb42e2 100644 --- a/assets/integration/woocommerce.js +++ b/assets/integration/woocommerce.js @@ -1 +1 @@ -function gtmkitLoad(){const a=window.gtmkit_settings.datalayer_name;let d;const n={"wp-block-handpicked-products":1,"wp-block-product-best-sellers":1,"wp-block-product-category":1,"wp-block-product-new":1,"wp-block-product-on-sale":1,"wp-block-products-by-attribute":1,"wp-block-product-tag":1,"wp-block-product-top-rated":1};document.querySelectorAll(".wc-block-grid .wc-block-grid__product").forEach(function(t){var e=t.closest(".wc-block-grid"),i=t.querySelector(".gtmkit_product_data");if(e&&i){var o,r=e.classList;if(r)for(const c in n)r.contains(c)&&((o=JSON.parse(i.getAttribute("data-gtmkit_product_data"))).item_list_name=window.gtmkit_settings.wc.text[c],o.index=n[c],i.setAttribute("data-gtmkit_product_data",JSON.stringify(o)),n[c]++)}});var t=document.querySelectorAll(".gtmkit_product_data");if(t.length){const i=[];let e;t.forEach(function(t){e=JSON.parse(t.getAttribute("data-gtmkit_product_data")),i.push(e)}),window[a].push({ecommerce:null}),window[a].push({event:"view_item_list",ecommerce:{items:i}})}document.addEventListener("click",function(t){t=t.target;let e;if(!t)return!0;if(t.closest(".add_to_cart_button.ajax_add_to_cart:not(.single_add_to_cart_button)"))e="add_to_cart";else{if(!t.closest(".products")&&!t.closest(".wc-block-grid__products")||!t.closest(".add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list)"))return!0;e="add_to_wishlist"}t=t.closest(".product,.wc-block-grid__product"),t=t&&t.querySelector(".gtmkit_product_data");if(!t)return!0;t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));window[a].push({ecommerce:null}),window[a].push({event:e,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}})}),document.addEventListener("click",function(o){o=o.target;if(!o)return!0;let r,i=o.closest("form.cart");if(!i){let t=o.parentNode;for(;!i&&t;)i=t.querySelector("form.cart"),t=t.parentNode}if(!i)return!0;o.closest(".single_add_to_cart_button:not(.disabled,.input-needed)")?r="add_to_cart":o.closest(".add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list,.disabled-add-wishlist)")&&(r="add_to_wishlist");var o=i.querySelectorAll("[name=variation_id]"),t=i.classList&&i.classList.contains("grouped_form");if(o.length){let t=1,e;d&&(o=i.querySelector("[name=quantity]"),d.quantity=o&&o.value||1,t=d.quantity,e=d.price),(d&&"add_to_cart"===r||"add_to_wishlist"===r)&&(window[a].push({ecommerce:null}),window[a].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:e*t,items:[d]}}))}else if(t){o=document.querySelectorAll(".grouped_form .gtmkit_product_data");const c=[];let i=0;if(o.forEach(function(t){var e=document.querySelectorAll("input[name=quantity\\["+t.getAttribute("data-gtmkit_product_id")+"\\]]");if(0===(e=parseInt(e[0].value)))return!0;t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));t.quantity=e,c.push(t),i+=t.price*t.quantity}),0===c.length)return!0;window[a].push({ecommerce:null}),window[a].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:i,items:c}})}else{t=JSON.parse(i.querySelector("[name=gtmkit_product_data]")&&i.querySelector("[name=gtmkit_product_data]").value);t.quantity=i.querySelector("[name=quantity]")&&i.querySelector("[name=quantity]").value,window[a].push({ecommerce:null}),window[a].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price*t.quantity,items:[t]}})}}),document.addEventListener("click",function(t){var t=t.target;return!t||!t.closest(".mini_cart_item a.remove,.product-remove a.remove")||!(t=JSON.parse(t.getAttribute("data-gtmkit_product_data")))||void window[a].push({event:"remove_from_cart",ecommerce:{items:[t]}})});document.addEventListener("click",function(t){t=t.target;if(!t.closest(".products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist,.tinvwl_add_to_wishlist_button),.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist,.tinvwl_add_to_wishlist_button),.woocommerce-grouped-product-list-item__label a:not(.add_to_wishlist,.tinvwl_add_to_wishlist_button)"))return!0;var t=t.closest(".product,.wc-block-grid__product");let e;return!t||!(e=t.querySelector(".gtmkit_product_data"))||void 0===e.getAttribute("data-gtmkit_product_data")||!(t=JSON.parse(e.getAttribute("data-gtmkit_product_data")))||void window[a].push({event:"select_item",ecommerce:{items:[t]}})}),jQuery(document).on("found_variation",function(t,e){if(void 0!==e){t=t.target;if(t.querySelector("[name=gtmkit_product_data]")){var t=JSON.parse(t.querySelector("[name=gtmkit_product_data]")&&t.querySelector("[name=gtmkit_product_data]").value),i=(t.id=t.item_id=window.gtmkit_settings.wc.pid_prefix+e.variation_id,window.gtmkit_settings.wc.use_sku&&e.sku&&""!==e.sku&&(t.id=t.item_id=window.gtmkit_settings.wc.pid_prefix+e.sku),t.price=e.display_price,[]);for(const o in e.attributes)i.push(e.attributes[o]);t.item_variant=i.filter(t=>t).join("|"),d=t,0!==window.gtmkit_settings.wc.view_item.config&&(window[a].push({ecommerce:null}),window[a].push({event:"view_item",ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}}))}}})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",gtmkitLoad):gtmkitLoad(); \ No newline at end of file +function gtmkitLoad(){const d=window.gtmkit_settings.datalayer_name;let a;const n={"wp-block-handpicked-products":1,"wp-block-product-best-sellers":1,"wp-block-product-category":1,"wp-block-product-new":1,"wp-block-product-on-sale":1,"wp-block-products-by-attribute":1,"wp-block-product-tag":1,"wp-block-product-top-rated":1};document.querySelectorAll(".wc-block-grid .wc-block-grid__product").forEach(function(t){var e=t.closest(".wc-block-grid"),i=t.querySelector(".gtmkit_product_data");if(e&&i){var o,r=e.classList;if(r)for(const c in n)r.contains(c)&&((o=JSON.parse(i.getAttribute("data-gtmkit_product_data"))).item_list_name=window.gtmkit_settings.wc.text[c],o.index=n[c],i.setAttribute("data-gtmkit_product_data",JSON.stringify(o)),n[c]++)}});var t=document.querySelectorAll(".gtmkit_product_data");if(t.length){const i=[];let e;t.forEach(function(t){e=JSON.parse(t.getAttribute("data-gtmkit_product_data")),i.push(e)}),window[d].push({ecommerce:null}),window[d].push({event:"view_item_list",ecommerce:{items:i}})}document.addEventListener("click",function(t){t=t.target;let e;if(!t)return!0;if(t.closest(".add_to_cart_button.ajax_add_to_cart:not(.single_add_to_cart_button)"))e="add_to_cart";else{if(!t.closest(".products")&&!t.closest(".wc-block-grid__products")||!t.closest(".add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list)"))return!0;e="add_to_wishlist"}t=t.closest(".product,.wc-block-grid__product"),t=t&&t.querySelector(".gtmkit_product_data");if(!t)return!0;t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));window[d].push({ecommerce:null}),window[d].push({event:e,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}})}),document.addEventListener("click",function(o){o=o.target;if(!o)return!0;let r,i=o.closest("form.cart");if(!i){let t=o.parentNode;for(;!i&&t;)i=t.querySelector("form.cart"),t=t.parentNode}if(!i)return!0;o.closest(".single_add_to_cart_button:not(.disabled,.input-needed)")?r="add_to_cart":o.closest(".add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list,.disabled-add-wishlist)")&&(r="add_to_wishlist");var o=i.querySelectorAll("[name=variation_id]"),t=i.classList&&i.classList.contains("grouped_form");if(o.length){let t=1,e;a&&(o=i.querySelector("[name=quantity]"),a.quantity=o&&o.value||1,t=a.quantity,e=a.price),(a&&"add_to_cart"===r||"add_to_wishlist"===r)&&(window[d].push({ecommerce:null}),window[d].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:e*t,items:[a]}}))}else if(t){o=document.querySelectorAll(".grouped_form .gtmkit_product_data");const c=[];let i=0;if(o.forEach(function(t){let e=document.querySelectorAll("input[name=quantity\\["+t.getAttribute("data-gtmkit_product_id")+"\\]]");if(e=Number(e[0].value),0===(e=isNaN(e)?0:e)&&"add_to_cart"===r)return!0;0===e&&"add_to_wishlist"===r&&(e=1);t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));t.quantity=e,c.push(t),i+=t.price*t.quantity}),0===c.length)return!0;window[d].push({ecommerce:null}),window[d].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:i,items:c}})}else{t=JSON.parse(i.querySelector("[name=gtmkit_product_data]")&&i.querySelector("[name=gtmkit_product_data]").value);t.quantity=i.querySelector("[name=quantity]")&&i.querySelector("[name=quantity]").value,window[d].push({ecommerce:null}),window[d].push({event:r,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price*t.quantity,items:[t]}})}}),document.addEventListener("click",function(t){var t=t.target;return!t||!t.closest(".mini_cart_item a.remove,.product-remove a.remove")||!(t=JSON.parse(t.getAttribute("data-gtmkit_product_data")))||void window[d].push({event:"remove_from_cart",ecommerce:{items:[t]}})});document.addEventListener("click",function(t){t=t.target;if(!t.closest(".products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist,.tinvwl_add_to_wishlist_button),.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist,.tinvwl_add_to_wishlist_button),.woocommerce-grouped-product-list-item__label a:not(.add_to_wishlist,.tinvwl_add_to_wishlist_button)"))return!0;var t=t.closest(".product,.wc-block-grid__product");let e;return!t||!(e=t.querySelector(".gtmkit_product_data"))||void 0===e.getAttribute("data-gtmkit_product_data")||!(t=JSON.parse(e.getAttribute("data-gtmkit_product_data")))||void window[d].push({event:"select_item",ecommerce:{items:[t]}})}),jQuery(document).on("found_variation",function(t,e){if(void 0!==e){t=t.target;if(t.querySelector("[name=gtmkit_product_data]")){var t=JSON.parse(t.querySelector("[name=gtmkit_product_data]")&&t.querySelector("[name=gtmkit_product_data]").value),i=(t.id=t.item_id=window.gtmkit_settings.wc.pid_prefix+e.variation_id,window.gtmkit_settings.wc.use_sku&&e.sku&&""!==e.sku&&(t.id=t.item_id=window.gtmkit_settings.wc.pid_prefix+e.sku),t.price=e.display_price,[]);for(const o in e.attributes)i.push(e.attributes[o]);t.item_variant=i.filter(t=>t).join("|"),a=t,0!==window.gtmkit_settings.wc.view_item.config&&(window[d].push({ecommerce:null}),window[d].push({event:"view_item",ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}}))}}})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",gtmkitLoad):gtmkitLoad(); \ No newline at end of file diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index a021b45..465ddff 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -838,7 +838,7 @@ public function get_item_data_tag( WC_Product $product, string $item_list_name, return sprintf( '', - esc_attr( $this->prefix_item_id( $product->get_id() ) ), + esc_attr( $product->get_id() ), esc_attr( wp_json_encode( $item_data ) ) ); } diff --git a/src/js/woocommerce.js b/src/js/woocommerce.js index 628c358..adb4e2d 100644 --- a/src/js/woocommerce.js +++ b/src/js/woocommerce.js @@ -198,10 +198,14 @@ function gtmkitLoad() { productData.getAttribute('data-gtmkit_product_id') + '\\]]' ); - productQuantity = parseInt(productQuantity[0].value); - if (0 === productQuantity) { + productQuantity = Number(productQuantity[0].value); + productQuantity = isNaN(productQuantity) ? 0 : productQuantity; + + if (0 === productQuantity && event === 'add_to_cart') { return true; + } else if (0 === productQuantity && event === 'add_to_wishlist') { + productQuantity = 1; } const itemData = JSON.parse(