Commit 49db96d188e for woocommerce
commit 49db96d188e9fe4fb875192f8055373411c28e8e
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date: Thu Nov 19 11:05:57 2026 -0300
Refuse unknown location codes in shipping-zone-create (#71076)
* Refuse unknown location codes in shipping-zone-create
The ability saved any country, state or continent code it got, so a
code such as UK or XX made a zone that never matches an address.
It now checks each code against WC()->countries, saves the code as
WooCommerce lists it, and names every bad code in one 400 error.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Check shipping zone country codes with country_exists()
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Drop the address-match claim from the changelog
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/wooai-1714-shipping-zone-location-codes b/plugins/woocommerce/changelog/wooai-1714-shipping-zone-location-codes
new file mode 100644
index 00000000000..c1e5cd1a79d
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1714-shipping-zone-location-codes
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Refuse unknown country, state and continent codes when an AI agent creates a shipping zone.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ShippingZoneCreate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ShippingZoneCreate.php
index 9b00c764d6b..443f4626147 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ShippingZoneCreate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ShippingZoneCreate.php
@@ -119,20 +119,34 @@ class ShippingZoneCreate extends AbstractDomainAbility implements AbilityDefinit
return new \WP_Error( 'woocommerce_ability_shipping_zone_name_empty', __( 'The zone name is empty.', 'woocommerce' ), array( 'status' => 400 ) );
}
+ $locations = array();
+ $invalid = array();
+ foreach ( $input['locations'] ?? array() as $location ) {
+ $code = sanitize_text_field( $location['code'] );
+ $known = self::get_known_location_code( $code, $location['type'] );
+ if ( null === $known ) {
+ $invalid[] = sprintf( '%s (%s)', $code, $location['type'] );
+ continue;
+ }
+ $locations[] = array(
+ 'code' => $known,
+ 'type' => $location['type'],
+ );
+ }
+
+ if ( ! empty( $invalid ) ) {
+ return new \WP_Error(
+ 'woocommerce_ability_shipping_zone_location_invalid',
+ /* translators: %s: location codes with their types, such as "UK (country), US:ZZ (state)". */
+ sprintf( __( 'WooCommerce does not know these location codes, or they do not fit their type: %s. Use ISO country codes such as GB, COUNTRY:STATE codes WooCommerce lists for the country, and continent codes such as EU.', 'woocommerce' ), implode( ', ', $invalid ) ),
+ array( 'status' => 400 )
+ );
+ }
+
$zone = new \WC_Shipping_Zone( null );
$zone->set_zone_name( $name );
$zone->set_zone_order( $input['order'] ?? 0 );
- $zone->set_locations(
- array_map(
- static function ( array $location ): array {
- return array(
- 'code' => sanitize_text_field( $location['code'] ),
- 'type' => $location['type'],
- );
- },
- $input['locations'] ?? array()
- )
- );
+ $zone->set_locations( $locations );
$zone->save();
if ( ! $zone->get_id() ) {
@@ -141,4 +155,38 @@ class ShippingZoneCreate extends AbstractDomainAbility implements AbilityDefinit
return array( 'zone' => self::format_zone_for_response( new \WC_Shipping_Zone( $zone->get_id() ) ) );
}
+
+ /**
+ * Get the code as WooCommerce lists it, matched without case, so a saved zone matches addresses.
+ * Postcode patterns pass as given. A state must be one WooCommerce lists for its country.
+ *
+ * @param string $code Location code.
+ * @param string $type Location type.
+ * @return string|null Null when WooCommerce does not know the code for the type.
+ */
+ private static function get_known_location_code( string $code, string $type ): ?string {
+ $countries = WC()->countries;
+ if ( 'country' === $type ) {
+ return $countries->country_exists( strtoupper( $code ) ) ? strtoupper( $code ) : null;
+ }
+
+ if ( 'continent' === $type ) {
+ $known = array_keys( $countries->get_continents() );
+ } elseif ( 'state' === $type ) {
+ $parts = explode( ':', $code, 2 );
+ $country = strtoupper( $parts[0] );
+ $states = isset( $parts[1] ) ? $countries->get_states( $country ) : array();
+ $known = array_map( static fn( $state ): string => $country . ':' . $state, array_keys( is_array( $states ) ? $states : array() ) );
+ } else {
+ return $code;
+ }
+
+ foreach ( $known as $candidate ) {
+ if ( 0 === strcasecmp( (string) $candidate, $code ) ) {
+ return (string) $candidate;
+ }
+ }
+
+ return null;
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingWritesTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingWritesTest.php
index 40a2b111e64..9c93d91ec76 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingWritesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingWritesTest.php
@@ -505,6 +505,128 @@ class ShippingWritesTest extends \WC_Unit_Test_Case {
$this->assertCount( $zone_count, \WC_Shipping_Zones::get_zones(), 'No zone should be created.' );
}
+ /**
+ * @testdox Should refuse a location code WooCommerce does not know, or one that does not fit its type, and save nothing.
+ * @testWith ["XX", "country"]
+ * ["UK", "country"]
+ * ["US:ZZ", "state"]
+ * ["DE:BY", "state"]
+ * ["XX", "continent"]
+ * ["US:CA", "country"]
+ * ["DE", "state"]
+ * ["EU", "country"]
+ *
+ * @param string $code Location code.
+ * @param string $type Location type.
+ */
+ public function test_refuses_unknown_location_codes( string $code, string $type ): void {
+ $zone_count = count( \WC_Shipping_Zones::get_zones() );
+
+ $result = $this->run_ability(
+ 'woocommerce/shipping-zone-create',
+ array(
+ 'name' => 'Nowhere',
+ 'locations' => array(
+ array(
+ 'code' => $code,
+ 'type' => $type,
+ ),
+ ),
+ )
+ );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'woocommerce_ability_shipping_zone_location_invalid', $result->get_error_code() );
+ $this->assertSame( 400, $result->get_error_data()['status'] );
+ $this->assertStringContainsString( "{$code} ({$type})", $result->get_error_message() );
+ $this->assertCount( $zone_count, \WC_Shipping_Zones::get_zones(), 'No zone should be created.' );
+ }
+
+ /**
+ * @testdox Should name every bad location code in one error.
+ */
+ public function test_names_every_bad_location_code_in_one_error(): void {
+ $result = $this->run_ability(
+ 'woocommerce/shipping-zone-create',
+ array(
+ 'name' => 'Mixed',
+ 'locations' => array(
+ array(
+ 'code' => 'UK',
+ 'type' => 'country',
+ ),
+ array(
+ 'code' => 'DE',
+ 'type' => 'country',
+ ),
+ array(
+ 'code' => 'US:ZZ',
+ 'type' => 'state',
+ ),
+ array(
+ 'code' => 'XX',
+ 'type' => 'continent',
+ ),
+ ),
+ )
+ );
+
+ $this->assertWPError( $result );
+ $message = $result->get_error_message();
+ foreach ( array( 'UK (country)', 'US:ZZ (state)', 'XX (continent)' ) as $bad ) {
+ $this->assertStringContainsString( $bad, $message );
+ }
+ $this->assertStringNotContainsString( 'DE (country)', $message, 'A valid code should not be named.' );
+ }
+
+ /**
+ * @testdox Should save known codes of each type in the form WooCommerce stores, so the zone matches an address.
+ */
+ public function test_saves_known_location_codes_in_stored_form(): void {
+ $result = $this->run_ability(
+ 'woocommerce/shipping-zone-create',
+ array(
+ 'name' => 'Known',
+ 'locations' => array(
+ array(
+ 'code' => 'gb',
+ 'type' => 'country',
+ ),
+ array(
+ 'code' => 'us:ca',
+ 'type' => 'state',
+ ),
+ array(
+ 'code' => 'MA:MAAGD',
+ 'type' => 'state',
+ ),
+ array(
+ 'code' => 'eu',
+ 'type' => 'continent',
+ ),
+ array(
+ 'code' => 'sw1a*',
+ 'type' => 'postcode',
+ ),
+ ),
+ )
+ );
+
+ $this->assertNotWPError( $result );
+ $this->assertSame(
+ array( 'GB', 'US:CA', 'MA:maagd', 'EU', 'SW1A*' ),
+ array_column( $result['zone']['locations'], 'code' )
+ );
+ $package = array(
+ 'destination' => array(
+ 'country' => 'GB',
+ 'state' => '',
+ 'postcode' => 'SW1A 1AA',
+ ),
+ );
+ $this->assertSame( $result['zone']['id'], \WC_Shipping_Zones::get_zone_matching_package( $package )->get_id() );
+ }
+
/**
* @testdox Should refuse a zone name with nothing left after cleaning.
*/