Add simulate cart hooks

This commit is contained in:
Pedro Silva 2023-10-12 18:22:55 +01:00
parent 6f6b3ac1c1
commit b29da6a627
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
3 changed files with 21 additions and 3 deletions

View file

@ -17,7 +17,7 @@ class SingleProductBootstap {
this.formSelector = 'form.cart';
// Prevent simulate cart being called too many times in a burst.
this.simulateCartThrottled = throttle(this.simulateCart, 5000);
this.simulateCartThrottled = throttle(this.simulateCart, this.gateway.simulate_cart.throttling || 5000);
this.renderer.onButtonsInit(this.gateway.button.wrapper, () => {
this.handleChange();
@ -217,6 +217,11 @@ class SingleProductBootstap {
}
simulateCart() {
// Check of cart simulation is enabled.
if (!this.gateway.simulate_cart.enabled) {
return;
}
const actionHandler = new SingleProductActionHandler(
null,
null,

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

@ -159,8 +159,17 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
// Remove from cart because some plugins reserve resources internally when adding to cart.
$this->remove_cart_items();
// Unset cart to prevent it being updated/persisted.
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 );
}