From ad7a49df4c7b616b9ed57da144775ae7dd092784 Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:38:54 +0100 Subject: [PATCH] Prevent error on Cart Shipping Calculator when using Additional Checkout Fields API (#49685) --- .../cart-checkout/shipping-calculator/address.tsx | 8 +++++++- .../changelog/fix-shipping-calculator-custom-fields | 4 ++++ .../src/StoreApi/Schemas/V1/AbstractAddressSchema.php | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-shipping-calculator-custom-fields diff --git a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-calculator/address.tsx b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-calculator/address.tsx index 50c596a2803..d97eb1a78d3 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-calculator/address.tsx +++ b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/shipping-calculator/address.tsx @@ -73,7 +73,13 @@ const ShippingCalculatorAddress = ( { const isAddressValid = validateSubmit(); if ( isAddressValid ) { - return onUpdate( address ); + const addressToSubmit = {}; + addressFields.forEach( ( key ) => { + if ( typeof address[ key ] !== 'undefined' ) { + addressToSubmit[ key ] = address[ key ]; + } + } ); + return onUpdate( addressToSubmit ); } } } type="submit" diff --git a/plugins/woocommerce/changelog/fix-shipping-calculator-custom-fields b/plugins/woocommerce/changelog/fix-shipping-calculator-custom-fields new file mode 100644 index 00000000000..d51c135c794 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-shipping-calculator-custom-fields @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Prevent a warning showing when using the Shipping Address Calculator in combination with the additional checkout fields API diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/AbstractAddressSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/AbstractAddressSchema.php index 3d77a9e2aaa..2ecf7ebc3e7 100644 --- a/plugins/woocommerce/src/StoreApi/Schemas/V1/AbstractAddressSchema.php +++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/AbstractAddressSchema.php @@ -172,6 +172,13 @@ abstract class AbstractAddressSchema extends AbstractSchema { // correct format, and finally the second validation step is to ensure the correctly-formatted values // match what we expect (postcode etc.). foreach ( $address as $key => $value ) { + + // Only run specific validation on properties that are defined in the schema and present in the address. + // This is for partial address pushes when only part of a customer address is sent. + // Full schema address validation still happens later, so empty, required values are disallowed. + if ( empty( $schema[ $key ] ) || empty( $address[ $key ] ) ) { + continue; + } if ( is_wp_error( rest_validate_value_from_schema( $value, $schema[ $key ], $key ) ) ) { $errors->add( 'invalid_' . $key,