Skip to content

Commit a2f6466

Browse files
committed
Fix(button): Prevent WooCommerce notices leaking during cart simulation
WooCommerce stores notices in the global session, not on the cart instance. During PayPal cart simulation, `add_to_cart()` may trigger notices (e.g. "Please choose product options") which then persist and are shown to the user. This change snapshots existing notices before simulation and restores them afterward, ensuring simulation-generated notices do not leak into the user session while preserving legitimate notices.
1 parent 8ee5cd2 commit a2f6466

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

modules/ppcp-button/src/Helper/IsolatedCartSimulator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function simulate( array $products ): array {
4444
};
4545

4646
add_filter( 'woocommerce_paypal_payments_is_simulating_cart', $simulation_filter );
47+
$existing_notices = wc_get_notices();
4748

4849
try {
4950
$cart = $this->create_isolated_cart();
@@ -62,6 +63,15 @@ public function simulate( array $products ): array {
6263
throw $e;
6364
} finally {
6465
remove_filter( 'woocommerce_paypal_payments_is_simulating_cart', $simulation_filter );
66+
$current_notices = wc_get_notices();
67+
68+
/**
69+
* Simulation may add WooCommerce notices (stored in the global session, not the cart).
70+
* Snapshot + restore ensures simulation notices don't leak into the user session.
71+
*/
72+
if ( $current_notices !== $existing_notices ) {
73+
wc_set_notices( $existing_notices );
74+
}
6575

6676
if ( $cart instanceof WC_Cart ) {
6777
$this->cleanup_cart( $cart );

0 commit comments

Comments
 (0)