mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Add simulate cart hooks
This commit is contained in:
parent
6f6b3ac1c1
commit
b29da6a627
3 changed files with 21 additions and 3 deletions
|
@ -17,7 +17,7 @@ class SingleProductBootstap {
|
||||||
this.formSelector = 'form.cart';
|
this.formSelector = 'form.cart';
|
||||||
|
|
||||||
// Prevent simulate cart being called too many times in a burst.
|
// 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.renderer.onButtonsInit(this.gateway.button.wrapper, () => {
|
||||||
this.handleChange();
|
this.handleChange();
|
||||||
|
@ -217,6 +217,11 @@ class SingleProductBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
simulateCart() {
|
simulateCart() {
|
||||||
|
// Check of cart simulation is enabled.
|
||||||
|
if (!this.gateway.simulate_cart.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const actionHandler = new SingleProductActionHandler(
|
const actionHandler = new SingleProductActionHandler(
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|
|
@ -963,6 +963,10 @@ class SmartButton implements SmartButtonInterface {
|
||||||
// phpcs:ignore WordPress.WP.I18n
|
// phpcs:ignore WordPress.WP.I18n
|
||||||
'shipping_field' => _x( 'Shipping %s', 'checkout-validation', 'woocommerce' ),
|
'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,
|
'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' ),
|
'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' ),
|
'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ),
|
||||||
|
|
|
@ -159,8 +159,17 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
|
||||||
// Remove from cart because some plugins reserve resources internally when adding to cart.
|
// Remove from cart because some plugins reserve resources internally when adding to cart.
|
||||||
$this->remove_cart_items();
|
$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 );
|
unset( WC()->cart );
|
||||||
|
} else {
|
||||||
|
// Restores cart, may lead to race conditions.
|
||||||
|
if ( null !== $this->real_cart ) {
|
||||||
|
WC()->cart = $this->real_cart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unset( $this->cart );
|
unset( $this->cart );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue