diff --git a/src/StoreApi/Schemas/BillingAddressSchema.php b/src/StoreApi/Schemas/BillingAddressSchema.php index bdff2d209d4..051b3dd2a33 100644 --- a/src/StoreApi/Schemas/BillingAddressSchema.php +++ b/src/StoreApi/Schemas/BillingAddressSchema.php @@ -93,6 +93,14 @@ public function validate_callback( $address, $request, $param ) { */ public function get_item_response( $address ) { if ( ( $address instanceof \WC_Customer || $address instanceof \WC_Order ) ) { + $billing_country = $address->get_billing_country(); + $billing_state = $address->get_billing_state(); + $valid_states = wc()->countries->get_states( $billing_country ); + + if ( ! empty( $billing_state ) && count( $valid_states ) && ! in_array( $billing_state, $valid_states, true ) ) { + $billing_state = ''; + } + return (object) $this->prepare_html_response( [ 'first_name' => $address->get_billing_first_name(), @@ -101,9 +109,9 @@ public function get_item_response( $address ) { 'address_1' => $address->get_billing_address_1(), 'address_2' => $address->get_billing_address_2(), 'city' => $address->get_billing_city(), - 'state' => $address->get_billing_state(), + 'state' => $billing_state, 'postcode' => $address->get_billing_postcode(), - 'country' => $address->get_billing_country(), + 'country' => $billing_country, 'email' => $address->get_billing_email(), 'phone' => $address->get_billing_phone(), ] diff --git a/src/StoreApi/Schemas/ShippingAddressSchema.php b/src/StoreApi/Schemas/ShippingAddressSchema.php index 922c714a11b..bf89ec2577e 100644 --- a/src/StoreApi/Schemas/ShippingAddressSchema.php +++ b/src/StoreApi/Schemas/ShippingAddressSchema.php @@ -42,6 +42,14 @@ public function get_item_response( $address ) { $shipping_phone = $address->get_meta( $address instanceof \WC_Customer ? 'shipping_phone' : '_shipping_phone', true ); } + $shipping_country = $address->get_shipping_country(); + $shipping_state = $address->get_shipping_state(); + $valid_states = wc()->countries->get_states( $shipping_country ); + + if ( ! empty( $shipping_state ) && count( $valid_states ) && ! in_array( $shipping_state, $valid_states, true ) ) { + $shipping_state = ''; + } + return (object) $this->prepare_html_response( [ 'first_name' => $address->get_shipping_first_name(), @@ -50,9 +58,9 @@ public function get_item_response( $address ) { 'address_1' => $address->get_shipping_address_1(), 'address_2' => $address->get_shipping_address_2(), 'city' => $address->get_shipping_city(), - 'state' => $address->get_shipping_state(), + 'state' => $shipping_state, 'postcode' => $address->get_shipping_postcode(), - 'country' => $address->get_shipping_country(), + 'country' => $shipping_country, 'phone' => $shipping_phone, ] );