From e2f6e1a52d0287b3f5c1d8bae8bea727f06e8576 Mon Sep 17 00:00:00 2001 From: Oluwaseun Olorunsola Date: Fri, 26 Sep 2025 17:51:54 +0100 Subject: [PATCH 1/2] fix(classic): improve error handling for product ID retrieval in classicTracking --- assets/js/src/integrations/classic.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/js/src/integrations/classic.js b/assets/js/src/integrations/classic.js index 5d33c689..061af81c 100644 --- a/assets/js/src/integrations/classic.js +++ b/assets/js/src/integrations/classic.js @@ -60,13 +60,15 @@ export function classicTracking( } // Get product ID from data attribute (archive pages) or value (single product pages). const productID = parseInt( - button?.[ 0 ]?.dataset.product_id || button?.[ 0 ]?.value + button?.[ 0 ]?.dataset?.product_id || button?.[ 0 ]?.value ); if ( Number.isNaN( productID ) ) { - throw new Error( + // eslint-disable-next-line no-console + console.error( 'Google Analytics for WooCommerce: Could not read product ID from the button given in `added_to_cart` event. Check whether WooCommerce Core events or elements are malformed by other extensions.' ); + return; } // If the current product doesn't match search by ID. @@ -102,12 +104,14 @@ export function classicTracking( * @param {HTMLElement|Object} element - The HTML element clicked on to trigger this event */ function removeFromCartHandler( element ) { - const productID = parseInt( element.target?.dataset.product_id ); + const productID = parseInt( element.target?.dataset?.product_id ); if ( Number.isNaN( productID ) ) { - throw new Error( + // eslint-disable-next-line no-console + console.error( 'Google Analytics for WooCommerce: Could not read product ID from the target element given to remove from cart event. Check whether WooCommerce Core events or elements are malformed by other extensions.' ); + return; } getEventHandler( 'remove_from_cart' )( { product: getProductFromID( productID, products, cart ), From 6b1f360db74b4341792a46a1661e7169322919e8 Mon Sep 17 00:00:00 2001 From: Oluwaseun Olorunsola Date: Mon, 29 Sep 2025 09:36:29 +0100 Subject: [PATCH 2/2] fix(classic): streamline product ID retrieval by removing optional chaining in classicTracking --- assets/js/src/integrations/classic.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/src/integrations/classic.js b/assets/js/src/integrations/classic.js index 061af81c..aa7aecdb 100644 --- a/assets/js/src/integrations/classic.js +++ b/assets/js/src/integrations/classic.js @@ -60,7 +60,7 @@ export function classicTracking( } // Get product ID from data attribute (archive pages) or value (single product pages). const productID = parseInt( - button?.[ 0 ]?.dataset?.product_id || button?.[ 0 ]?.value + button?.[ 0 ]?.dataset.product_id || button?.[ 0 ]?.value ); if ( Number.isNaN( productID ) ) { @@ -104,7 +104,7 @@ export function classicTracking( * @param {HTMLElement|Object} element - The HTML element clicked on to trigger this event */ function removeFromCartHandler( element ) { - const productID = parseInt( element.target?.dataset?.product_id ); + const productID = parseInt( element.target?.dataset.product_id ); if ( Number.isNaN( productID ) ) { // eslint-disable-next-line no-console