From aac5606b817b8c1c46f7d80ed65ac59a12d0e33f Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Mon, 13 May 2024 16:00:29 +0100 Subject: [PATCH 1/4] Prioritise products object when searching by ID --- assets/js/src/utils/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/src/utils/index.js b/assets/js/src/utils/index.js index dc314b00..789c209c 100644 --- a/assets/js/src/utils/index.js +++ b/assets/js/src/utils/index.js @@ -178,7 +178,7 @@ const formatCategoryKey = ( index ) => { */ export const getProductFromID = ( search, products, cart ) => { return ( - cart?.items?.find( ( { id } ) => id === search ) ?? - products?.find( ( { id } ) => id === search ) + products?.find( ( { id } ) => id === search ) ?? + cart?.items?.find( ( { id } ) => id === search ) ); }; From 523932fb711428ae4bd56830ca59ba91b70ee3a4 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Mon, 13 May 2024 16:04:26 +0100 Subject: [PATCH 2/4] Include quantity when product is added to cart --- includes/class-wc-abstract-google-analytics-js.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-abstract-google-analytics-js.php b/includes/class-wc-abstract-google-analytics-js.php index 38844cf7..d89aca51 100644 --- a/includes/class-wc-abstract-google-analytics-js.php +++ b/includes/class-wc-abstract-google-analytics-js.php @@ -80,7 +80,7 @@ function () { add_action( 'woocommerce_add_to_cart', function ( $cart_item_key, $product_id, $quantity, $variation_id, $variation ) { - $this->set_script_data( 'added_to_cart', $this->get_formatted_product( wc_get_product( $product_id ), $variation_id, $variation ) ); + $this->set_script_data( 'added_to_cart', $this->get_formatted_product( wc_get_product( $product_id ), $variation_id, $variation, $quantity ) ); }, 10, 5 @@ -211,7 +211,7 @@ function ( $item ) { * * @return array */ - public function get_formatted_product( WC_Product $product, $variation_id = 0, $variation = false ): array { + public function get_formatted_product( WC_Product $product, $variation_id = 0, $variation = false, $quantity = false ): array { $product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(); $price = $product->get_price(); @@ -241,6 +241,10 @@ public function get_formatted_product( WC_Product $product, $variation_id = 0, $ ), ); + if ( $quantity ) { + $formatted['quantity'] = (int) $quantity; + } + if ( $product->is_type( 'variation' ) ) { $variation = $product->get_attributes(); } From 5976d7408486736abbce7802909d7ad1d6db4c70 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Mon, 13 May 2024 16:04:36 +0100 Subject: [PATCH 3/4] Add E2E tests --- .../specs/gtag-events/blocks-pages.test.js | 35 +++++++++++++++++++ .../specs/gtag-events/classic-pages.test.js | 26 ++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/e2e/specs/gtag-events/blocks-pages.test.js b/tests/e2e/specs/gtag-events/blocks-pages.test.js index 404f6c87..27663534 100644 --- a/tests/e2e/specs/gtag-events/blocks-pages.test.js +++ b/tests/e2e/specs/gtag-events/blocks-pages.test.js @@ -225,6 +225,41 @@ test.describe( 'GTag events on block pages', () => { } ); } ); + test( 'Add to cart has correct quantity when product is already in cart', async ( { + page, + } ) => { + const addToCart = `[data-product_id="${ simpleProductID }"]`; + + await createProductsBlockShopPage(); + await page.goto( `products-block-shop` ); + + const addToCartButton = await page.locator( addToCart ).first(); + + await addToCartButton.click(); + await expect( addToCartButton.getByText( '1 in cart' ) ).toBeVisible(); + await addToCartButton.click(); + await expect( addToCartButton.getByText( '2 in cart' ) ).toBeVisible(); + + await page.reload(); + + const event = trackGtagEvent( page, 'add_to_cart' ); + + const addToCartButton2 = await page.locator( addToCart ).first(); + await addToCartButton2.click(); + await expect( addToCartButton.getByText( '3 in cart' ) ).toBeVisible(); + + await event.then( ( request ) => { + const data = getEventData( request, 'add_to_cart' ); + expect( data.product1 ).toEqual( { + id: simpleProductID.toString(), + nm: 'Simple product', + ca: 'Uncategorized', + qt: '1', + pr: simpleProductPrice.toString(), + } ); + } ); + } ); + test( 'View item list event is sent from the products block shop page', async ( { page, } ) => { diff --git a/tests/e2e/specs/gtag-events/classic-pages.test.js b/tests/e2e/specs/gtag-events/classic-pages.test.js index c1bc2f93..f2770b2c 100644 --- a/tests/e2e/specs/gtag-events/classic-pages.test.js +++ b/tests/e2e/specs/gtag-events/classic-pages.test.js @@ -74,6 +74,32 @@ test.describe( 'GTag events on classic pages', () => { } ); } ); + test( 'Add to cart quantity is sent on a single product page', async ( { + page, + } ) => { + const event = trackGtagEvent( page, 'add_to_cart' ); + + await page.goto( `?p=${ simpleProductID }` ); + + await page.locator( '.quantity input.qty' ).first().fill('3'); + + const addToCart = `.single_add_to_cart_button[value="${ simpleProductID }"]`; + const addToCartButton = await page.locator( addToCart ).first(); + + await addToCartButton.click(); + + await event.then( ( request ) => { + const data = getEventData( request, 'add_to_cart' ); + expect( data.product1 ).toEqual( { + id: simpleProductID.toString(), + nm: 'Simple product', + ca: 'Uncategorized', + qt: '3', + pr: simpleProductPrice.toString(), + } ); + } ); + } ); + test( 'Add to cart event is sent on the home page when adding product through URL', async ( { page, } ) => { From 9f997db520c06bc3f5e7141c2136f22cab301f2b Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Mon, 13 May 2024 16:55:52 +0100 Subject: [PATCH 4/4] CS Fixes --- includes/class-wc-abstract-google-analytics-js.php | 1 + tests/e2e/specs/gtag-events/blocks-pages.test.js | 2 +- tests/e2e/specs/gtag-events/classic-pages.test.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-abstract-google-analytics-js.php b/includes/class-wc-abstract-google-analytics-js.php index d89aca51..902c408e 100644 --- a/includes/class-wc-abstract-google-analytics-js.php +++ b/includes/class-wc-abstract-google-analytics-js.php @@ -208,6 +208,7 @@ function ( $item ) { * @param int $variation_id Variation product ID. * @param array|bool $variation An array containing product variation attributes to include in the product data. * For the "variation" type products, we'll use product->get_attributes. + * @param bool|int $quantity Quantity to include in the formatted product object * * @return array */ diff --git a/tests/e2e/specs/gtag-events/blocks-pages.test.js b/tests/e2e/specs/gtag-events/blocks-pages.test.js index 27663534..5ba2213d 100644 --- a/tests/e2e/specs/gtag-events/blocks-pages.test.js +++ b/tests/e2e/specs/gtag-events/blocks-pages.test.js @@ -232,7 +232,7 @@ test.describe( 'GTag events on block pages', () => { await createProductsBlockShopPage(); await page.goto( `products-block-shop` ); - + const addToCartButton = await page.locator( addToCart ).first(); await addToCartButton.click(); diff --git a/tests/e2e/specs/gtag-events/classic-pages.test.js b/tests/e2e/specs/gtag-events/classic-pages.test.js index f2770b2c..fcfb0add 100644 --- a/tests/e2e/specs/gtag-events/classic-pages.test.js +++ b/tests/e2e/specs/gtag-events/classic-pages.test.js @@ -81,7 +81,7 @@ test.describe( 'GTag events on classic pages', () => { await page.goto( `?p=${ simpleProductID }` ); - await page.locator( '.quantity input.qty' ).first().fill('3'); + await page.locator( '.quantity input.qty' ).first().fill( '3' ); const addToCart = `.single_add_to_cart_button[value="${ simpleProductID }"]`; const addToCartButton = await page.locator( addToCart ).first();