Skip to content

Commit

Permalink
Move som CSS selectors from JS to PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Jan 9, 2024
1 parent 2c455c4 commit 7b48c0a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion assets/integration/woocommerce.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ 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'] = [
'singe_product_wishlist' => '.add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list,.disabled-add-wishlist)',
'product_list_select_item' => '.products .product: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)',
'product_list_wishlist' => '.add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list)',
];

return $global_settings;
}
Expand Down
10 changes: 3 additions & 7 deletions src/js/woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function gtmkitLoad() {
(eventTargetElement.closest('.products') ||
eventTargetElement.closest('.wc-block-grid__products')) &&
eventTargetElement.closest(
'.add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list)'
window.gtmkit_settings.wc.css_selectors.product_list_wishlist
)
) {
event = 'add_to_wishlist';
Expand Down Expand Up @@ -148,7 +148,7 @@ function gtmkitLoad() {
if ( formCartElement ) {
if ( eventTargetElement.closest( '.single_add_to_cart_button:not(.disabled,.input-needed)' ) ) {
event = 'add_to_cart';
} else if ( eventTargetElement.closest( '.add_to_wishlist, .tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list,.disabled-add-wishlist)' ) ) {
} else if ( eventTargetElement.closest( window.gtmkit_settings.wc.css_selectors.single_product_wishlist ) ) {
event = 'add_to_wishlist';
} else {
return true;
Expand Down Expand Up @@ -287,13 +287,9 @@ function gtmkitLoad() {
});

// select_item event on clicks in product lists
const productListItemSelector =
'.products .product: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)';
document.addEventListener('click', function (e) {
const eventTargetElement = e.target;
const linkElement = eventTargetElement.closest(productListItemSelector);
const linkElement = eventTargetElement.closest( window.gtmkit_settings.wc.css_selectors.product_list_select_item );
if (!linkElement) return true;

const product = eventTargetElement.closest(
Expand Down

0 comments on commit 7b48c0a

Please sign in to comment.