Merge pull request #1753 from woocommerce/PCP-2066-cart-simulation-improvements

Cart simulation improvements (2066)
This commit is contained in:
Emili Castells 2023-10-13 15:04:47 +02:00 committed by GitHub
commit 327a87dfe8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 4 deletions

View file

@ -963,6 +963,10 @@ class SmartButton implements SmartButtonInterface {
// phpcs:ignore WordPress.WP.I18n
'shipping_field' => _x( 'Shipping %s', 'checkout-validation', 'woocommerce' ),
),
'simulate_cart' => array(
'enabled' => apply_filters( 'woocommerce_paypal_payments_simulate_cart_enabled', true ),
'throttling' => apply_filters( 'woocommerce_paypal_payments_simulate_cart_throttling', 5000 ),
),
'order_id' => 'pay-now' === $this->context() ? $this->get_order_pay_id() : 0,
'single_product_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'product' ),
'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ),

View file

@ -151,6 +151,7 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
/**
* Restores the real cart.
* Currently, unsets the WC cart to prevent race conditions arising from it being persisted.
*
* @return void
*/
@ -158,10 +159,17 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
// Remove from cart because some plugins reserve resources internally when adding to cart.
$this->remove_cart_items();
// Restore cart and unset cart clone.
if ( null !== $this->real_cart ) {
WC()->cart = $this->real_cart;
if ( apply_filters( 'woocommerce_paypal_payments_simulate_cart_prevent_updates', true ) ) {
// Removes shutdown actions to prevent persisting session, transients and save cookies.
remove_all_actions( 'shutdown' );
unset( WC()->cart );
} else {
// Restores cart, may lead to race conditions.
if ( null !== $this->real_cart ) {
WC()->cart = $this->real_cart;
}
}
unset( $this->cart );
}