Add throttling to simulate_cart ajax call
This commit is contained in:
Pedro Silva 2023-07-20 17:51:48 +01:00
parent a0480e35bb
commit 84a362a7c5
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
13 changed files with 203 additions and 114 deletions

View file

@ -1384,7 +1384,7 @@ class SmartButton implements SmartButtonInterface {
* Checks if PayPal buttons/messages should be rendered for the current page.
*
* @param string|null $context The context that should be checked, use default otherwise.
* @param float|null $price_total The price total to be considered.
* @param float|null $price_total The price total to be considered.
* @return bool
*/
public function is_button_disabled( string $context = null, float $price_total = null ): bool {
@ -1429,7 +1429,7 @@ class SmartButton implements SmartButtonInterface {
/**
* Checks a filter if pay_later/messages should be rendered on a given location / context.
*
* @param string $location The location.
* @param string $location The location.
* @param float|null $price_total The price total to be considered.
* @return bool
*/
@ -1476,7 +1476,7 @@ class SmartButton implements SmartButtonInterface {
/**
* Check whether Pay Later button is enabled for a given location.
*
* @param string $location The location.
* @param string $location The location.
* @param float|null $price_total The price total to be considered.
* @return bool true if is enabled, otherwise false.
*/
@ -1489,7 +1489,7 @@ class SmartButton implements SmartButtonInterface {
/**
* Check whether Pay Later message is enabled for a given location.
*
* @param string $location The location setting name.
* @param string $location The location setting name.
* @param float|null $price_total The price total to be considered.
* @return bool true if is enabled, otherwise false.
*/

View file

@ -1,4 +1,9 @@
<?php
/**
* Abstract class for cart Endpoints.
*
* @package WooCommerce\PayPalCommerce\Button\Endpoint
*/
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
@ -6,6 +11,9 @@ use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
/**
* Abstract Class AbstractCartEndpoint
*/
abstract class AbstractCartEndpoint implements EndpointInterface {
const ENDPOINT = '';
@ -50,7 +58,7 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
*
* @var array
*/
private $cart_item_keys = [];
private $cart_item_keys = array();
/**
* The nonce.
@ -97,7 +105,7 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
*
* @param array $products Array of products to be added to cart.
* @return bool
* @throws Exception
* @throws Exception Add to cart methods throw an exception on fail.
*/
protected function add_products( array $products ): bool {
$this->cart->empty_cart( false );
@ -106,20 +114,20 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
foreach ( $products as $product ) {
if ( $product['product']->is_type( 'booking' ) ) {
$success = $success && $this->add_booking_product(
$product['product'],
$product['booking']
);
$product['product'],
$product['booking']
);
} elseif ( $product['product']->is_type( 'variable' ) ) {
$success = $success && $this->add_variable_product(
$product['product'],
$product['quantity'],
$product['variations']
);
$product['product'],
$product['quantity'],
$product['variations']
);
} else {
$success = $success && $this->add_product(
$product['product'],
$product['quantity']
);
$product['product'],
$product['quantity']
);
}
}
@ -164,6 +172,8 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
}
/**
* Returns product information from request data.
*
* @return array|false
*/
protected function products_from_request() {
@ -304,6 +314,8 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
}
/**
* Removes stored cart items from WooCommerce cart.
*
* @return void
*/
protected function remove_cart_items(): void {

View file

@ -65,7 +65,7 @@ class CartScriptParamsEndpoint implements EndpointInterface {
*/
public function handle_request(): bool {
try {
if ( is_callable('wc_maybe_define_constant') ) {
if ( is_callable( 'wc_maybe_define_constant' ) ) {
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
}

View file

@ -16,7 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
/**
* Class ChangeCartEndpoint
*/
class ChangeCartEndpoint extends AbstractCartEndpoint {
class ChangeCartEndpoint extends AbstractCartEndpoint {
const ENDPOINT = 'ppc-change-cart';
@ -70,13 +70,15 @@ class ChangeCartEndpoint extends AbstractCartEndpoint {
* @throws Exception On error.
*/
protected function handle_data(): bool {
if ( ! $products = $this->products_from_request() ) {
$products = $this->products_from_request();
if ( ! $products ) {
return false;
}
$this->shipping->reset_shipping();
if ( ! $this->add_products($products) ) {
if ( ! $this->add_products( $products ) ) {
return false;
}

View file

@ -59,40 +59,43 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
* @throws Exception On error.
*/
protected function handle_data(): bool {
if ( ! $products = $this->products_from_request() ) {
$products = $this->products_from_request();
if ( ! $products ) {
return false;
}
// Set WC default cart as the clone.
// Store a reference to the real cart.
$activeCart = WC()->cart;
WC()->cart = $this->cart;
$active_cart = WC()->cart;
WC()->cart = $this->cart;
if ( ! $this->add_products($products) ) {
if ( ! $this->add_products( $products ) ) {
return false;
}
$this->cart->calculate_totals();
$total = (float) $this->cart->get_total( 'numeric' );
// Remove from cart because some plugins reserve resources internally when adding to cart.
$this->remove_cart_items();
// Restore cart and unset cart clone
WC()->cart = $activeCart;
// Restore cart and unset cart clone.
WC()->cart = $active_cart;
unset( $this->cart );
wp_send_json_success(
array(
'total' => $total,
'funding' => [
'paylater' => [
'enabled' => $this->smart_button->is_pay_later_button_enabled_for_location( 'cart', $total ),
'total' => $total,
'funding' => array(
'paylater' => array(
'enabled' => $this->smart_button->is_pay_later_button_enabled_for_location( 'cart', $total ),
'messaging_enabled' => $this->smart_button->is_pay_later_messaging_enabled_for_location( 'cart', $total ),
]
],
'button' => [
),
),
'button' => array(
'is_disabled' => $this->smart_button->is_button_disabled( 'cart', $total ),
]
),
)
);
return true;