Merge branch 'trunk' into PCP-1967-paylater

This commit is contained in:
Alex P 2023-09-08 11:15:50 +03:00
commit ec0b4b8c65
No known key found for this signature in database
GPG key ID: 54487A734A204D71
86 changed files with 5079 additions and 1421 deletions

View file

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

View file

@ -25,6 +25,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
@ -330,6 +331,8 @@ class CreateOrderEndpoint implements EndpointInterface {
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
$wc_order->save_meta_data();
do_action( 'woocommerce_paypal_payments_woocommerce_order_created', $wc_order, $order );
}
wp_send_json_success( $this->make_response( $order ) );

View file

@ -27,6 +27,13 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
*/
private $smart_button;
/**
* The WooCommerce real active cart.
*
* @var \WC_Cart|null
*/
private $real_cart = null;
/**
* ChangeCartEndpoint constructor.
*
@ -65,24 +72,14 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
return false;
}
// Set WC default cart as the clone.
// Store a reference to the real cart.
$active_cart = WC()->cart;
WC()->cart = $this->cart;
$this->replace_real_cart();
if ( ! $this->add_products( $products ) ) {
return false;
}
$this->add_products( $products );
$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 = $active_cart;
unset( $this->cart );
$this->restore_real_cart();
// Process filters.
$pay_later_enabled = true;
@ -119,4 +116,44 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
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 );
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Helper;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -163,6 +164,10 @@ class EarlyOrderHandler {
/**
* Patch Order so we have the \WC_Order id added.
*/
return $this->order_processor->patch_order( $wc_order, $order );
$order = $this->order_processor->patch_order( $wc_order, $order );
do_action( 'woocommerce_paypal_payments_woocommerce_order_created', $wc_order, $order );
return $order;
}
}