Checkout: Missing country error for virtual orders (edge case) (https://github.com/woocommerce/woocommerce-blocks/pull/6050)
* Make shipping address field optional on checkout routes * Make update_customer_from_request use billing for shipping if omitted * making shipping optional when needed Co-authored-by: Nadir Seghir <nadir.seghir@gmail.com>
This commit is contained in:
parent
c401369e04
commit
f926b24480
|
@ -48,7 +48,7 @@ const CheckoutProcessor = () => {
|
|||
const { hasValidationErrors } = useValidationContext();
|
||||
const { shippingErrorStatus } = useShippingDataContext();
|
||||
const { billingData, shippingAddress } = useCustomerDataContext();
|
||||
const { cartNeedsPayment, receiveCart } = useStoreCart();
|
||||
const { cartNeedsPayment, cartNeedsShipping, receiveCart } = useStoreCart();
|
||||
const {
|
||||
activePaymentMethod,
|
||||
isExpressPaymentMethodActive,
|
||||
|
@ -185,15 +185,18 @@ const CheckoutProcessor = () => {
|
|||
billing_address: emptyHiddenAddressFields(
|
||||
currentBillingData.current
|
||||
),
|
||||
shipping_address: emptyHiddenAddressFields(
|
||||
currentShippingAddress.current
|
||||
),
|
||||
customer_note: orderNotes,
|
||||
create_account: shouldCreateAccount,
|
||||
...paymentData,
|
||||
extensions: { ...extensionData },
|
||||
};
|
||||
|
||||
if ( cartNeedsShipping ) {
|
||||
data.shipping_address = emptyHiddenAddressFields(
|
||||
currentShippingAddress.current
|
||||
);
|
||||
}
|
||||
|
||||
triggerFetch( {
|
||||
path: '/wc/store/v1/checkout',
|
||||
method: 'POST',
|
||||
|
@ -265,14 +268,15 @@ const CheckoutProcessor = () => {
|
|||
}, [
|
||||
isProcessingOrder,
|
||||
removeNotice,
|
||||
orderNotes,
|
||||
shouldCreateAccount,
|
||||
cartNeedsPayment,
|
||||
paymentMethodId,
|
||||
paymentMethodData,
|
||||
shouldSavePayment,
|
||||
activePaymentMethod,
|
||||
orderNotes,
|
||||
shouldCreateAccount,
|
||||
extensionData,
|
||||
cartNeedsShipping,
|
||||
dispatchActions,
|
||||
addErrorNotice,
|
||||
receiveCart,
|
||||
|
|
|
@ -393,21 +393,21 @@ class Checkout extends AbstractCartRoute {
|
|||
private function update_customer_from_request( \WP_REST_Request $request ) {
|
||||
$customer = wc()->customer;
|
||||
|
||||
if ( isset( $request['billing_address'] ) ) {
|
||||
foreach ( $request['billing_address'] as $key => $value ) {
|
||||
if ( is_callable( [ $customer, "set_billing_$key" ] ) ) {
|
||||
$customer->{"set_billing_$key"}( $value );
|
||||
}
|
||||
// Billing address is a required field.
|
||||
foreach ( $request['billing_address'] as $key => $value ) {
|
||||
if ( is_callable( [ $customer, "set_billing_$key" ] ) ) {
|
||||
$customer->{"set_billing_$key"}( $value );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $request['shipping_address'] ) ) {
|
||||
foreach ( $request['shipping_address'] as $key => $value ) {
|
||||
if ( is_callable( [ $customer, "set_shipping_$key" ] ) ) {
|
||||
$customer->{"set_shipping_$key"}( $value );
|
||||
} elseif ( 'phone' === $key ) {
|
||||
$customer->update_meta_data( 'shipping_phone', $value );
|
||||
}
|
||||
// If shipping address (optional field) was not provided, set it to the given billing address (required field).
|
||||
$shipping_address_values = $request['shipping_address'] ?? $request['billing_address'];
|
||||
|
||||
foreach ( $shipping_address_values as $key => $value ) {
|
||||
if ( is_callable( [ $customer, "set_shipping_$key" ] ) ) {
|
||||
$customer->{"set_shipping_$key"}( $value );
|
||||
} elseif ( 'phone' === $key ) {
|
||||
$customer->update_meta_data( 'shipping_phone', $value );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,6 @@ class CheckoutSchema extends AbstractSchema {
|
|||
'sanitize_callback' => [ $this->shipping_address_schema, 'sanitize_callback' ],
|
||||
'validate_callback' => [ $this->shipping_address_schema, 'validate_callback' ],
|
||||
],
|
||||
'required' => true,
|
||||
],
|
||||
'payment_method' => [
|
||||
'description' => __( 'The ID of the payment method being used to process the payment.', 'woo-gutenberg-products-block' ),
|
||||
|
|
Loading…
Reference in New Issue