Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Enforce valid states from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Oct 19, 2021
1 parent 39c2296 commit e3aea72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/StoreApi/Schemas/BillingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
]
Expand Down
12 changes: 10 additions & 2 deletions src/StoreApi/Schemas/ShippingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
]
);
Expand Down

0 comments on commit e3aea72

Please sign in to comment.