Skip to content

Commit

Permalink
Fixed: track AJAX (i.e. non-interactivity API) add to cart events for…
Browse files Browse the repository at this point in the history
… non-block themes.
  • Loading branch information
Dan0sz committed Jun 1, 2024
1 parent 9114820 commit 04fb33a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class WooCommerce {
'cart_total',
'cart_total_items',
'customer_id',
'id',
'name',
'order_id',
'price',
'product_id',
'product_name',
'quantity',
'shipping',
'subtotal',
Expand Down Expand Up @@ -76,6 +75,7 @@ private function init( $init ) {
*/
add_filter( 'woocommerce_after_add_to_cart_form', [ $this, 'track_add_to_cart_on_product_page' ] );
add_filter( 'woocommerce_store_api_validate_add_to_cart', [ $this, 'track_add_to_cart' ], 10, 2 );
add_filter( 'woocommerce_ajax_added_to_cart', [ $this, 'track_ajax_add_to_cart' ] );
add_action( 'woocommerce_remove_cart_item', [ $this, 'track_remove_cart_item' ], 10, 2 );
add_action( 'wp_head', [ $this, 'track_entered_checkout' ] );
add_action( 'woocommerce_thankyou', [ $this, 'track_purchase' ] );
Expand Down Expand Up @@ -146,6 +146,25 @@ public function track_add_to_cart_on_product_page() {
}

/**
* Track (non-Interactivity API, i.e. AJAX) add to cart events.
*
* @param string|int $product_id ID of the product added to the cart.
*
* @return void
*/
public function track_ajax_add_to_cart( $product_id ) {
$product = wc_get_product( $product_id );
$add_to_cart_data = [
'id' => $product_id,
'quantity' => $_POST[ 'quantity' ] ?? 1,
];

$this->track_add_to_cart( $product, $add_to_cart_data );
}

/**
* Track regular (i.e. interactivity API) add to cart events.
*
* @param WC_Product $product General information about the product added to cart.
* @param array $add_to_cart_data Cart data for the product added to the cart, e.g. quantity, variation ID, etc.
*
Expand Down

0 comments on commit 04fb33a

Please sign in to comment.