Refactor error and exception handing on SimulateCart

This commit is contained in:
Pedro Silva 2023-09-04 11:56:22 +01:00
parent 2571d31ad8
commit 7ff9dcd2d4
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
2 changed files with 74 additions and 25 deletions

View file

@ -153,9 +153,10 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
/** /**
* Handles errors. * Handles errors.
* *
* @param bool $send_response If this error handling should return the response.
* @return void * @return void
*/ */
private function handle_error(): void { protected function handle_error( bool $send_response = true ): void {
$message = __( $message = __(
'Something went wrong. Action aborted', 'Something went wrong. Action aborted',
@ -173,14 +174,16 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
wc_clear_notices(); wc_clear_notices();
} }
wp_send_json_error( if ( $send_response ) {
array( wp_send_json_error(
'name' => '', array(
'message' => $message, 'name' => '',
'code' => 0, 'message' => $message,
'details' => array(), 'code' => 0,
) 'details' => array(),
); )
);
}
} }
/** /**
@ -259,7 +262,9 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
private function add_product( \WC_Product $product, int $quantity ): bool { private function add_product( \WC_Product $product, int $quantity ): bool {
$cart_item_key = $this->cart->add_to_cart( $product->get_id(), $quantity ); $cart_item_key = $this->cart->add_to_cart( $product->get_id(), $quantity );
$this->cart_item_keys[] = $cart_item_key; if ( $cart_item_key ) {
$this->cart_item_keys[] = $cart_item_key;
}
return false !== $cart_item_key; return false !== $cart_item_key;
} }
@ -294,7 +299,9 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
$variations $variations
); );
$this->cart_item_keys[] = $cart_item_key; if ( $cart_item_key ) {
$this->cart_item_keys[] = $cart_item_key;
}
return false !== $cart_item_key; return false !== $cart_item_key;
} }
@ -322,7 +329,9 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
$cart_item_key = $this->cart->add_to_cart( $product->get_id(), 1, 0, array(), $cart_item_data ); $cart_item_key = $this->cart->add_to_cart( $product->get_id(), 1, 0, array(), $cart_item_data );
$this->cart_item_keys[] = $cart_item_key; if ( $cart_item_key ) {
$this->cart_item_keys[] = $cart_item_key;
}
return false !== $cart_item_key; return false !== $cart_item_key;
} }
@ -333,6 +342,9 @@ abstract class AbstractCartEndpoint implements EndpointInterface {
*/ */
protected function remove_cart_items(): void { protected function remove_cart_items(): void {
foreach ( $this->cart_item_keys as $cart_item_key ) { foreach ( $this->cart_item_keys as $cart_item_key ) {
if ( ! $cart_item_key ) {
continue;
}
$this->cart->remove_cart_item( $cart_item_key ); $this->cart->remove_cart_item( $cart_item_key );
} }
} }

View file

@ -27,6 +27,13 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
*/ */
private $smart_button; private $smart_button;
/**
* The WooCommerce real active cart.
*
* @var \WC_Cart|null
*/
private $real_cart = null;
/** /**
* ChangeCartEndpoint constructor. * ChangeCartEndpoint constructor.
* *
@ -65,24 +72,14 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
return false; return false;
} }
// Set WC default cart as the clone. $this->replace_real_cart();
// Store a reference to the real cart.
$active_cart = WC()->cart;
WC()->cart = $this->cart;
if ( ! $this->add_products( $products ) ) { $this->add_products( $products );
return false;
}
$this->cart->calculate_totals(); $this->cart->calculate_totals();
$total = (float) $this->cart->get_total( 'numeric' ); $total = (float) $this->cart->get_total( 'numeric' );
// Remove from cart because some plugins reserve resources internally when adding to cart. $this->restore_real_cart();
$this->remove_cart_items();
// Restore cart and unset cart clone.
WC()->cart = $active_cart;
unset( $this->cart );
// Process filters. // Process filters.
$pay_later_enabled = true; $pay_later_enabled = true;
@ -119,4 +116,44 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
return true; return true;
} }
/**
* Handles errors.
*
* @param bool $send_response If this error handling should return the response.
* @return void
*
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
*/
protected function handle_error( bool $send_response = false ): void {
parent::handle_error( $send_response );
}
/**
* Replaces the real cart with the clone.
*
* @return void
*/
private function replace_real_cart() {
// Set WC default cart as the clone.
// Store a reference to the real cart.
$this->real_cart = WC()->cart;
WC()->cart = $this->cart;
}
/**
* Restores the real cart.
*
* @return void
*/
private function restore_real_cart() {
// 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;
}
unset( $this->cart );
}
} }