Skip to content

Commit

Permalink
only track cart updates after totals are calculated, rather than on c…
Browse files Browse the repository at this point in the history
…art updated events (which are done before totals are calculated)
  • Loading branch information
diosmosis committed Jan 23, 2025
1 parent 2334fbc commit 2a2177d
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions classes/WpMatomo/Ecommerce/Woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
class Woocommerce extends Base {
private $order_status_ignore = MATOMO_WOOCOMMERCE_IGNORED_ORDER_STATUS;

private $track_next_totals_change = false;

public function register_hooks() {
parent::register_hooks();

Expand All @@ -40,6 +42,7 @@ public function register_hooks() {

$this->logger->log('called from: ' . (new \Exception())->getTraceAsString());
} );
add_action( 'woocommerce_after_calculate_totals', [ $this, 'after_calculate_totals' ], 99999, 0 );

if ( ! $this->should_track_background() ) {
// prevent possibly executing same event twice where eg first a PHP Matomo tracker request is created
Expand All @@ -57,8 +60,24 @@ public function register_hooks() {
);
}

add_action( 'woocommerce_applied_coupon', [ $this, 'on_coupon_updated_safe' ], 99999, 0 );
add_action( 'woocommerce_removed_coupon', [ $this, 'on_coupon_updated_safe' ], 99999, 0 );
add_action( 'woocommerce_applied_coupon', [ $this, 'on_cart_updated_safe' ], 99999, 0 );
add_action( 'woocommerce_removed_coupon', [ $this, 'on_cart_updated_safe' ], 99999, 0 );
}

public function after_calculate_totals() {
if ( ! $this->track_next_totals_change ) {
return null;
}

try {
$val = $this->on_cart_updated( false );
} catch ( \Exception $e ) {
$this->logger->log_exception( 'woo_on_cart_update', $e );
} finally {
$this->track_next_totals_change = false;
}

return $val;
}

public function on_order_status_change( $order_id, $old_status, $new_status ) {
Expand Down Expand Up @@ -108,25 +127,8 @@ public function maybe_track_order_complete() {
}
}

public function on_coupon_updated_safe() {
try {
$val = null;
$val = $this->on_cart_updated( $val, true );
} catch ( \Exception $e ) {
$this->logger->log_exception( 'woo_on_cart_update', $e );
}

return $val;
}

public function on_cart_updated_safe( $val = null ) {
try {
$val = $this->on_cart_updated( $val );
} catch ( \Exception $e ) {
$this->logger->log_exception( 'woo_on_cart_update', $e );
}

return $val;
public function on_cart_updated_safe() {
$this->track_next_totals_change = true;
}

/**
Expand Down

0 comments on commit 2a2177d

Please sign in to comment.