Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cart-add-item hook in WC 8.5 #2192

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions js/src/gtag-events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading