Skip to content

Commit

Permalink
Improve support of add_to_wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Nov 30, 2023
1 parent 7aee345 commit 79c0653
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 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.

55 changes: 21 additions & 34 deletions src/js/woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,58 +126,45 @@ function gtmkitLoad() {
// add_to_cart event on product page
document.addEventListener('click', function (e) {
const eventTargetElement = e.target;
let event;

if (!eventTargetElement) {
return true;
}

let addToCart;
addToCart = eventTargetElement.closest('form.cart');
let event;
let formCartElement = eventTargetElement.closest('form.cart');

if (
addToCart &&
eventTargetElement.closest(
'.single_add_to_cart_button:not(.disabled,.input-needed)'
)
) {
event = 'add_to_cart';
} else if (
addToCart &&
eventTargetElement.closest(
'.tinvwl_add_to_wishlist_button:not(.tinvwl-product-in-list,.disabled-add-wishlist)'
)
) {
event = 'add_to_wishlist';
} else {
const addToWishlist = eventTargetElement.closest(
'.yith-wcwl-add-to-wishlist'
);
if ( ! formCartElement ) {
let parentNode = eventTargetElement.parentNode;

if (addToWishlist) {
addToCart = addToWishlist.parentNode.querySelector('form.cart');
if (addToCart) {
event = 'add_to_wishlist';
}
while (!formCartElement && parentNode) {
formCartElement = parentNode.querySelector('form.cart');
parentNode = parentNode.parentNode;
}
}

if (!event) {
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)' ) ) {
event = 'add_to_wishlist';
}
} else {
return true;
}

const productVariantId = addToCart.querySelectorAll(
const productVariantId = formCartElement.querySelectorAll(
'[name=variation_id]'
);
const productIsGrouped =
addToCart.classList && addToCart.classList.contains('grouped_form');
formCartElement.classList && formCartElement.classList.contains('grouped_form');

if (productVariantId.length) {
let quantity = 1;
let price;
if (selectedProductVariationData) {
const quantityElement =
addToCart.querySelector('[name=quantity]');
formCartElement.querySelector('[name=quantity]');
selectedProductVariationData.quantity =
(quantityElement && quantityElement.value) || 1;
quantity = selectedProductVariationData.quantity;
Expand Down Expand Up @@ -242,13 +229,13 @@ function gtmkitLoad() {
});
} else {
const itemData = JSON.parse(
addToCart.querySelector('[name=gtmkit_product_data]') &&
addToCart.querySelector('[name=gtmkit_product_data]').value
formCartElement.querySelector('[name=gtmkit_product_data]') &&
formCartElement.querySelector('[name=gtmkit_product_data]').value
);

itemData.quantity =
addToCart.querySelector('[name=quantity]') &&
addToCart.querySelector('[name=quantity]').value;
formCartElement.querySelector('[name=quantity]') &&
formCartElement.querySelector('[name=quantity]').value;

window[datalayerName].push({ ecommerce: null });
window[datalayerName].push({
Expand Down

0 comments on commit 79c0653

Please sign in to comment.