From 4cbbfa35662813d1539d7f34109b9dd2d2ac152a Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Sat, 28 Sep 2024 15:11:47 +0200 Subject: [PATCH] Add support for the Bricks theme builder --- assets/integration/woocommerce.js | 2 +- readme.txt | 1 + src/Common/Conditionals/BricksConditional.php | 23 +++++++++++++++++++ src/Integration/WooCommerce.php | 22 ++++++++++++++++-- src/js/woocommerce.js | 2 +- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + vendor/composer/installed.php | 4 ++-- 8 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/Common/Conditionals/BricksConditional.php diff --git a/assets/integration/woocommerce.js b/assets/integration/woocommerce.js index 751b5d1..9c4764d 100644 --- a/assets/integration/woocommerce.js +++ b/assets/integration/woocommerce.js @@ -1 +1 @@ -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 +function gtmkitLoad(){let s=window.gtmkit_settings.datalayer_name,n,d={"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 d)r.contains(o)&&((c=JSON.parse(i.getAttribute("data-gtmkit_product_data"))).item_list_name=window.gtmkit_settings.wc.text[o],c.index=d[o],i.setAttribute("data-gtmkit_product_data",JSON.stringify(c)),d[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[s].push({ecommerce:null}),window[s].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(window.gtmkit_settings.wc.css_selectors.product_list_add_to_cart))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[s].push({ecommerce:null}),window[s].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;n&&(r=i.querySelector("[name=quantity]"),n.quantity=r&&r.value||1,t=n.quantity,e=n.price),(n&&"add_to_cart"===o||"add_to_wishlist"===o)&&(window[s].push({ecommerce:null}),window[s].push({event:o,ecommerce:{currency:window.gtmkit_data.wc.currency,value:e*t,items:[n]}}))}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[s].push({ecommerce:null}),window[s].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[s].push({ecommerce:null}),window[s].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[s].push({ecommerce:null}),void window[s].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[s].push({ecommerce:null}),void window[s].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("|"),n=t,0!==window.gtmkit_settings.wc.view_item.config&&(window[s].push({ecommerce:null}),window[s].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(n))}}})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",gtmkitLoad):gtmkitLoad(); \ No newline at end of file diff --git a/readme.txt b/readme.txt index 2a8fd3f..4b4ba16 100644 --- a/readme.txt +++ b/readme.txt @@ -98,6 +98,7 @@ Release date: 2024-MM-DD Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-2-1/). #### Enhancements: +* Added support for the Bricks theme builder. #### Bugfixes: * In some cases other plugins or a theme could block GTM Kit from firing JS event like the one the fires the add_to_cart GTM event. diff --git a/src/Common/Conditionals/BricksConditional.php b/src/Common/Conditionals/BricksConditional.php new file mode 100644 index 0000000..951d316 --- /dev/null +++ b/src/Common/Conditionals/BricksConditional.php @@ -0,0 +1,23 @@ +get( 'Name' ) === 'Bricks' || wp_get_theme()->get( 'Template' ) === 'bricks' ); + } +} diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 7448104..08fa6fb 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -14,6 +14,7 @@ use Automattic\WooCommerce\StoreApi\Schemas\V1\CartItemSchema; use Automattic\WooCommerce\StoreApi\Schemas\V1\ProductSchema; use Exception; +use TLA_Media\GTM_Kit\Common\Conditionals\BricksConditional; use TLA_Media\GTM_Kit\Common\RestAPIServer; use TLA_Media\GTM_Kit\Common\Util; use TLA_Media\GTM_Kit\Options; @@ -244,6 +245,7 @@ public function get_global_settings( array $global_settings ): array { $global_settings['wc']['view_item']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_variable_product_tracking' ); $global_settings['wc']['view_item_list']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_view_item_list_limit' ); $global_settings['wc']['wishlist'] = false; + $global_settings['wc']['css_selectors'] = $this->get_css_selectors(); $global_settings['wc']['text'] = [ 'wp-block-handpicked-products' => __( 'Handpicked Products', 'gtm-kit' ), 'wp-block-product-best-sellers' => __( 'Best Sellers', 'gtm-kit' ), @@ -256,15 +258,31 @@ public function get_global_settings( array $global_settings ): array { 'shipping-tier-not-found' => __( 'Shipping tier not found', 'gtm-kit' ), 'payment-method-not-found' => __( 'Payment method not found', 'gtm-kit' ), ]; - $global_settings['wc']['css_selectors'] = [ + + return $global_settings; + } + + /** + * Get CSS Selectors + * + * @return array{product_list_select_item: string, product_list_element: string, product_list_exclude: string, product_list_add_to_cart: string} + */ + private function get_css_selectors(): array { + + $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)', 'product_list_element' => '.product,.wc-block-grid__product', 'product_list_exclude' => '', + 'product_list_add_to_cart' => '.add_to_cart_button.ajax_add_to_cart:not(.single_add_to_cart_button)', ]; - return $global_settings; + if ( ( new BricksConditional() )->is_met() ) { + $css_selectors['product_list_add_to_cart'] .= ',.add_to_cart_button.brx_ajax_add_to_cart:not(.single_add_to_cart_button)'; + } + + return $css_selectors; } /** diff --git a/src/js/woocommerce.js b/src/js/woocommerce.js index 810eab9..c2b4bbc 100644 --- a/src/js/woocommerce.js +++ b/src/js/woocommerce.js @@ -82,7 +82,7 @@ function gtmkitLoad() { if ( eventTargetElement.closest( - '.add_to_cart_button.ajax_add_to_cart:not(.single_add_to_cart_button)' + window.gtmkit_settings.wc.css_selectors.product_list_add_to_cart ) ) { event = 'add_to_cart'; diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index ccf4f95..15d6440 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -23,6 +23,7 @@ 'TLA_Media\\GTM_Kit\\Admin\\SetupWizard' => $baseDir . '/src/Admin/SetupWizard.php', 'TLA_Media\\GTM_Kit\\Admin\\TemplatesOptionsPage' => $baseDir . '/src/Admin/TemplatesOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\UpgradesOptionsPage' => $baseDir . '/src/Admin/UpgradesOptionsPage.php', + 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\BricksConditional' => $baseDir . '/src/Common/Conditionals/BricksConditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\Conditional' => $baseDir . '/src/Common/Conditionals/Conditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\ContactForm7Conditional' => $baseDir . '/src/Common/Conditionals/ContactForm7Conditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\EasyDigitalDownloadsConditional' => $baseDir . '/src/Common/Conditionals/EasyDigitalDownloadsConditional.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 2dd6dd1..3abc6c9 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -38,6 +38,7 @@ class ComposerStaticInit12fa396dcd6fc263a33fd78c6d8551b8 'TLA_Media\\GTM_Kit\\Admin\\SetupWizard' => __DIR__ . '/../..' . '/src/Admin/SetupWizard.php', 'TLA_Media\\GTM_Kit\\Admin\\TemplatesOptionsPage' => __DIR__ . '/../..' . '/src/Admin/TemplatesOptionsPage.php', 'TLA_Media\\GTM_Kit\\Admin\\UpgradesOptionsPage' => __DIR__ . '/../..' . '/src/Admin/UpgradesOptionsPage.php', + 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\BricksConditional' => __DIR__ . '/../..' . '/src/Common/Conditionals/BricksConditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\Conditional' => __DIR__ . '/../..' . '/src/Common/Conditionals/Conditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\ContactForm7Conditional' => __DIR__ . '/../..' . '/src/Common/Conditionals/ContactForm7Conditional.php', 'TLA_Media\\GTM_Kit\\Common\\Conditionals\\EasyDigitalDownloadsConditional' => __DIR__ . '/../..' . '/src/Common/Conditionals/EasyDigitalDownloadsConditional.php', diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index cabef30..2a9810a 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'tlamedia/gtm-kit', 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => 'b3228238bcfd8a33cbd843ee052f21ca9b557927', + 'reference' => 'c6e57338f099840ebaf09e6c6adb8df7c2fa34fe', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,7 +13,7 @@ 'tlamedia/gtm-kit' => array( 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => 'b3228238bcfd8a33cbd843ee052f21ca9b557927', + 'reference' => 'c6e57338f099840ebaf09e6c6adb8df7c2fa34fe', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(),