From 605558adc9af95d47adb3244f09118329cf20899 Mon Sep 17 00:00:00 2001 From: Miguel Perez Pellicer <5908855+puntope@users.noreply.github.com> Date: Mon, 1 Jan 2024 15:12:56 -0500 Subject: [PATCH] Support for cart-add-item in WC 8.5 --- js/src/gtag-events/index.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/js/src/gtag-events/index.js b/js/src/gtag-events/index.js index 6e2aaae887..5ea25b8838 100644 --- a/js/src/gtag-events/index.js +++ b/js/src/gtag-events/index.js @@ -16,13 +16,21 @@ import { /* global jQuery */ // Hook into cart add item events sent from Gutenberg blocks. -addAction( - `${ ACTION_PREFIX }-cart-add-item`, - NAMESPACE, - ( { product, quantity = 1 } ) => { - trackAddToCartEvent( product, quantity ); +// In WC >= 8.5 the product Proxy is the argument. +// In WC < 8.5 the product and the quantity are sent as arguments. +addAction( `${ ACTION_PREFIX }-cart-add-item`, NAMESPACE, ( data ) => { + if ( ! data ) { + return; + } + + if ( data?.product ) { + // WC < 8.5 + trackAddToCartEvent( data.product, data.quantity ); + } else { + // WC >= 8.5 + trackAddToCartEvent( data, 1 ); } -); +} ); /** * Handle add to cart clicks on any buttons shown in an archive loop.