Merge pull request #2437 from woocommerce/PCP-3395-when-there-isnt-any-shipping-option-for-the-address-the-order-is-still-created-from-classic-cart

When there isn't any shipping option for the address the order is still created from classic cart
This commit is contained in:
Emili Castells 2024-08-22 11:18:44 +02:00 committed by GitHub
commit 0115dec944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 15 deletions

View file

@ -20,10 +20,7 @@ const onApprove = ( context, errorHandler ) => {
} )
.then( ( data ) => {
if ( ! data.success ) {
errorHandler.genericError();
return actions.restart().catch( ( err ) => {
errorHandler.genericError();
} );
location.href = context.config.redirect;
}
const orderReceivedUrl = data.data?.order_received_url;

View file

@ -9,8 +9,10 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Helper;
use Exception;
use RuntimeException;
use WC_Cart;
use WC_Data_Exception;
use WC_Order;
use WC_Order_Item_Product;
use WC_Order_Item_Shipping;
@ -85,17 +87,22 @@ class WooCommerceOrderCreator {
throw new RuntimeException( 'Problem creating WC order.' );
}
$payer = $order->payer();
$shipping = $order->purchase_units()[0]->shipping();
try {
$payer = $order->payer();
$shipping = $order->purchase_units()[0]->shipping();
$this->configure_payment_source( $wc_order );
$this->configure_customer( $wc_order );
$this->configure_line_items( $wc_order, $wc_cart, $payer, $shipping );
$this->configure_shipping( $wc_order, $payer, $shipping );
$this->configure_coupons( $wc_order, $wc_cart->get_applied_coupons() );
$this->configure_payment_source( $wc_order );
$this->configure_customer( $wc_order );
$this->configure_line_items( $wc_order, $wc_cart, $payer, $shipping );
$this->configure_shipping( $wc_order, $payer, $shipping, $wc_cart );
$this->configure_coupons( $wc_order, $wc_cart->get_applied_coupons() );
$wc_order->calculate_totals();
$wc_order->save();
$wc_order->calculate_totals();
$wc_order->save();
} catch ( Exception $exception ) {
$wc_order->delete( true );
throw new RuntimeException( 'Failed to create WooCommerce order: ' . $exception->getMessage() );
}
return $wc_order;
}
@ -153,7 +160,7 @@ class WooCommerceOrderCreator {
$item->set_total( $subscription_total );
$subscription->add_product( $product );
$this->configure_shipping( $subscription, $payer, $shipping );
$this->configure_shipping( $subscription, $payer, $shipping, $wc_cart );
$this->configure_payment_source( $subscription );
$this->configure_coupons( $subscription, $wc_cart->get_applied_coupons() );
@ -178,9 +185,11 @@ class WooCommerceOrderCreator {
* @param WC_Order $wc_order The WC order.
* @param Payer|null $payer The payer.
* @param Shipping|null $shipping The shipping.
* @param WC_Cart $wc_cart The Cart.
* @return void
* @throws WC_Data_Exception|RuntimeException When failing to configure shipping.
*/
protected function configure_shipping( WC_Order $wc_order, ?Payer $payer, ?Shipping $shipping ): void {
protected function configure_shipping( WC_Order $wc_order, ?Payer $payer, ?Shipping $shipping, WC_Cart $wc_cart ): void {
$shipping_address = null;
$billing_address = null;
$shipping_options = null;
@ -218,6 +227,10 @@ class WooCommerceOrderCreator {
$shipping_options = $shipping->options()[0] ?? '';
}
if ( $wc_cart->needs_shipping() && empty( $shipping_options ) ) {
throw new RuntimeException( 'No shipping method has been selected.' );
}
if ( $shipping_address ) {
$wc_order->set_shipping_address( $shipping_address );
}