diff --git a/assets/integration/woocommerce.js b/assets/integration/woocommerce.js index 2264207..751b5d1 100644 --- a/assets/integration/woocommerce.js +++ b/assets/integration/woocommerce.js @@ -1 +1 @@ -function gtmkitLoad(){let a=window.gtmkit_settings.datalayer_name,d,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 r,c=e.classList;if(c)for(var o in n)c.contains(o)&&((r=JSON.parse(i.getAttribute("data-gtmkit_product_data"))).item_list_name=window.gtmkit_settings.wc.text[o],r.index=n[o],i.setAttribute("data-gtmkit_product_data",JSON.stringify(r)),n[o]++)}});var t=document.querySelectorAll(".gtmkit_product_data");if(t.length){let e=[],i;t.forEach(function(t){i=JSON.parse(t.getAttribute("data-gtmkit_product_data")),e.push(i)}),window[a].push({ecommerce:null}),window[a].push({event:"view_item_list",ecommerce:{items:e}})}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(!window.gtmkit_settings.wc.wishlist||!t.closest(".products")&&!t.closest(".wc-block-grid__products")||!t.closest(window.gtmkit_settings.wc.css_selectors.product_list_wishlist))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"));t.quantity=1,window[a].push({ecommerce:null}),window[a].push({event:e,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}})},!0),document.addEventListener("click",function(c){c=c.target;if(!c)return!0;let o,i=c.closest("form.cart");if(!i){let t=c.parentNode;for(;!i&&t;)i=t.querySelector("form.cart"),t=t.parentNode}if(!i)return!0;if(c.closest(".single_add_to_cart_button:not(.disabled,.input-needed)"))o="add_to_cart";else{if(!window.gtmkit_settings.wc.wishlist||!c.closest(window.gtmkit_settings.wc.css_selectors.single_product_wishlist))return!0;o="add_to_wishlist"}var c=i.querySelectorAll("[name=variation_id]"),t=i.classList&&i.classList.contains("grouped_form")&&!i.classList.contains("bundle_form");if(c.length){let t=1,e;d&&(c=i.querySelector("[name=quantity]"),d.quantity=c&&c.value||1,t=d.quantity,e=d.price),(d&&"add_to_cart"===o||"add_to_wishlist"===o)&&(window[a].push({ecommerce:null}),window[a].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:e*t,items:[d]}}))}else if(t){c=document.querySelectorAll(".grouped_form .gtmkit_product_data");let i=[],r=0;if(c.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"===o)return!0;0===e&&"add_to_wishlist"===o&&(e=1);t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));t.quantity=e,i.push(t),r+=t.price*t.quantity}),0===i.length)return!0;window[a].push({ecommerce:null}),window[a].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:r,items:i}})}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:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price*t.quantity,items:[t]}})}},!0),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")))||(window[a].push({ecommerce:null}),void window[a].push({event:"remove_from_cart",ecommerce:{items:[t]}}))},!0),document.addEventListener("click",function(t){t=t.target;if(!t.closest(window.gtmkit_settings.wc.css_selectors.product_list_select_item))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")))||(window[a].push({ecommerce:null}),void window[a].push({event:"select_item",ecommerce:{items:[t]}}))},!0),jQuery(document).on("found_variation",function(t,e){if(void 0!==e){t=t.target;if(t.querySelector("[name=gtmkit_product_data]")){var i,t=JSON.parse(t.querySelector("[name=gtmkit_product_data]")&&t.querySelector("[name=gtmkit_product_data]").value),r=(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(i in e.attributes)r.push(e.attributes[i]);t.item_variant=r.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]}})),!0===window.gtmkit_settings.console_log&&(console.log("GTM Kit: selectedProductVariationData"),console.log(d))}}})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",gtmkitLoad):gtmkitLoad(); \ No newline at end of file +function gtmkitLoad(){let n=window.gtmkit_settings.datalayer_name,s,a={"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 c,r=e.classList;if(r)for(var o in a)r.contains(o)&&((c=JSON.parse(i.getAttribute("data-gtmkit_product_data"))).item_list_name=window.gtmkit_settings.wc.text[o],c.index=a[o],i.setAttribute("data-gtmkit_product_data",JSON.stringify(c)),a[o]++)}});var t=document.querySelectorAll(".gtmkit_product_data");if(t.length){let e=[],i;t.forEach(function(t){i=JSON.parse(t.getAttribute("data-gtmkit_product_data")),e.push(i)}),window[n].push({ecommerce:null}),window[n].push({event:"view_item_list",ecommerce:{items:e}})}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(!window.gtmkit_settings.wc.wishlist||!t.closest(".products")&&!t.closest(".wc-block-grid__products")||!t.closest(window.gtmkit_settings.wc.css_selectors.product_list_wishlist))return!0;e="add_to_wishlist"}t=t.closest(window.gtmkit_settings.wc.css_selectors.product_list_element),t=t&&t.querySelector(".gtmkit_product_data");if(!t)return!0;t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));t.quantity=1,window[n].push({ecommerce:null}),window[n].push({event:e,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}})},!0),document.addEventListener("click",function(r){r=r.target;if(!r)return!0;let o,i=r.closest("form.cart");if(!i){let t=r.parentNode;for(;!i&&t;)i=t.querySelector("form.cart"),t=t.parentNode}if(!i)return!0;if(r.closest(".single_add_to_cart_button:not(.disabled,.input-needed)"))o="add_to_cart";else{if(!window.gtmkit_settings.wc.wishlist||!r.closest(window.gtmkit_settings.wc.css_selectors.single_product_wishlist))return!0;o="add_to_wishlist"}var r=i.querySelectorAll("[name=variation_id]"),t=i.classList&&i.classList.contains("grouped_form")&&!i.classList.contains("bundle_form");if(r.length){let t=1,e;s&&(r=i.querySelector("[name=quantity]"),s.quantity=r&&r.value||1,t=s.quantity,e=s.price),(s&&"add_to_cart"===o||"add_to_wishlist"===o)&&(window[n].push({ecommerce:null}),window[n].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:e*t,items:[s]}}))}else if(t){r=document.querySelectorAll(".grouped_form .gtmkit_product_data");let i=[],c=0;if(r.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"===o)return!0;0===e&&"add_to_wishlist"===o&&(e=1);t=JSON.parse(t.getAttribute("data-gtmkit_product_data"));t.quantity=e,i.push(t),c+=t.price*t.quantity}),0===i.length)return!0;window[n].push({ecommerce:null}),window[n].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:c,items:i}})}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[n].push({ecommerce:null}),window[n].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price*t.quantity,items:[t]}})}},!0),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")))||(window[n].push({ecommerce:null}),void window[n].push({event:"remove_from_cart",ecommerce:{items:[t]}}))},!0),document.addEventListener("click",function(t){var t=t.target,e=t.closest(window.gtmkit_settings.wc.css_selectors.product_list_select_item);if(!e)return!0;if(window.gtmkit_settings.wc.css_selectors.product_list_exclude&&e.closest(window.gtmkit_settings.wc.css_selectors.product_list_exclude))return!0;e=t.closest(window.gtmkit_settings.wc.css_selectors.product_list_element);let i;return!e||!(i=e.querySelector(".gtmkit_product_data"))||void 0===i.getAttribute("data-gtmkit_product_data")||!(t=JSON.parse(i.getAttribute("data-gtmkit_product_data")))||(window[n].push({ecommerce:null}),void window[n].push({event:"select_item",ecommerce:{items:[t]}}))},!0),jQuery(document).on("found_variation",function(t,e){if(void 0!==e){t=t.target;if(t.querySelector("[name=gtmkit_product_data]")){var i,t=JSON.parse(t.querySelector("[name=gtmkit_product_data]")&&t.querySelector("[name=gtmkit_product_data]").value),c=(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(i in e.attributes)c.push(e.attributes[i]);t.item_variant=c.filter(t=>t).join("|"),s=t,0!==window.gtmkit_settings.wc.view_item.config&&(window[n].push({ecommerce:null}),window[n].push({event:"view_item",ecommerce:{currency:window.gtmkit_data.wc.currency,value:t.price,items:[t]}})),!0===window.gtmkit_settings.console_log&&(console.log("GTM Kit: selectedProductVariationData"),console.log(s))}}})}"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 2a20075..0dd3f97 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -258,8 +258,10 @@ public function get_global_settings( array $global_settings ): array { ]; $global_settings['wc']['css_selectors'] = [ 'product_list_select_item' => '.products .product:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist),' . - '.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist),' . - '.woocommerce-grouped-product-list-item__label a:not(.add_to_wishlist)', + '.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button.ajax_add_to_cart,.add_to_wishlist),' . + '.woocommerce-grouped-product-list-item__label a:not(.add_to_wishlist)', + 'product_list_element' => '.product,.wc-block-grid__product', + 'product_list_exclude' => '', ]; return $global_settings; diff --git a/src/js/woocommerce.js b/src/js/woocommerce.js index 4c682ab..810eab9 100644 --- a/src/js/woocommerce.js +++ b/src/js/woocommerce.js @@ -99,7 +99,7 @@ function gtmkitLoad() { } const productElement = eventTargetElement.closest( - '.product,.wc-block-grid__product' + window.gtmkit_settings.wc.css_selectors.product_list_element ); const productData = productElement && @@ -292,8 +292,12 @@ function gtmkitLoad() { const linkElement = eventTargetElement.closest( window.gtmkit_settings.wc.css_selectors.product_list_select_item ); if (!linkElement) return true; + if ( window.gtmkit_settings.wc.css_selectors.product_list_exclude && linkElement.closest(window.gtmkit_settings.wc.css_selectors.product_list_exclude)) { + return true; + } + const product = eventTargetElement.closest( - '.product,.wc-block-grid__product' + window.gtmkit_settings.wc.css_selectors.product_list_element ); let productData;