From c2784c0338724c1dac2917a97d231ad266d0e5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Wed, 5 Jul 2023 15:56:12 +0200 Subject: [PATCH] Listen for car `remove` on the enclosing container, as cart form and items are being re-rendered. Fixes https://github.com/woocommerce/woocommerce-google-analytics-integration/issues/282 --- includes/class-wc-google-gtag-js.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-google-gtag-js.php b/includes/class-wc-google-gtag-js.php index 1820cf5d..cfceb20e 100644 --- a/includes/class-wc-google-gtag-js.php +++ b/includes/class-wc-google-gtag-js.php @@ -394,8 +394,12 @@ public function remove_from_cart() { }]}' ); + // To track all the consecutive removals, + // we listen for clicks on `.woocommerce` container(s), + // as `.woocommerce-cart-form` and its items are re-rendered on each removal. wc_enqueue_js( - "$( '.remove' ).off('click', '.remove').on( 'click', function() { + "const selector = '.woocommerce-cart-form__cart-item .remove'; + $( '.woocommerce' ).off('click', selector).on( 'click', selector, function() { $event_code });" );