Skip to content

Commit

Permalink
Avoid overwriting third-party event handlers
Browse files Browse the repository at this point in the history
Co-authored-by: Tomek Wytrębowicz <[email protected]>
  • Loading branch information
martynmjones and tomalec authored Feb 27, 2024
1 parent c979cf0 commit e893d8c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions assets/js/src/integrations/classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ export const trackClassicIntegration = () => {

// Attach event listeners on initial page load and when the cart div is updated
removeFromCartListener();
document.body.onupdated_wc_div = () => removeFromCartListener();
const oldOnupdatedWcDiv = document.body.onupdated_wc_div;
document.body.onupdated_wc_div = ( ...args ) => {
if ( typeof oldOnupdatedWcDiv === 'function' ) {
oldOnupdatedWcDiv( ...args );
}
removeFromCartListener();
};

// Trigger the handler when an item is removed from the mini-cart and WooCommerce dispatches the `removed_from_cart` event.
document.body.onremoved_from_cart = (
event,
fragments,
/* eslint-disable-next-line camelcase */
cart_hash,
button
) => removeFromCartHandler( { target: button[ 0 ] } );
const oldOnRemovedFromCart = document.body.onremoved_from_cart;
document.body.onremoved_from_cart = ( ...args ) => {
if ( typeof oldOnRemovedFromCart === 'function' ) {
oldOnRemovedFromCart( ...args );
}
removeFromCartHandler( { target: args[ 3 ][ 0 ] } );
};

/**
* Attaches click event listeners to non-block product listings that sends a
Expand Down

0 comments on commit e893d8c

Please sign in to comment.