From b29da6a62749b6bc7d493ffbcd3bd116cfa75be3 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Thu, 12 Oct 2023 18:22:55 +0100
Subject: [PATCH] Add simulate cart hooks
---
.../ContextBootstrap/SingleProductBootstap.js | 7 ++++++-
modules/ppcp-button/src/Assets/SmartButton.php | 4 ++++
.../src/Endpoint/SimulateCartEndpoint.php | 13 +++++++++++--
3 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js
index 5ff7f2a80..c8b7e386f 100644
--- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js
+++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js
@@ -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,
diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php
index 426bb9251..6b29d1dc7 100644
--- a/modules/ppcp-button/src/Assets/SmartButton.php
+++ b/modules/ppcp-button/src/Assets/SmartButton.php
@@ -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' ),
diff --git a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
index 171dc6c81..25ac4ba91 100644
--- a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
+++ b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
@@ -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.
- unset( WC()->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 );
}