Commit ff332d4d96d for woocommerce
commit ff332d4d96d62d5493a09992ca8f060dea302b54
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 22:00:28 2026 +0100
Charge shipping for checkout towns and states named 0 (#71268)
Forwardport of 543-gh-Automattic/woocommerce.
Source PR head: 619f30e8670fe6abfc351e5cfc28967c6ca3a386
Co-authored-by: Taha Paksu <3295+tpaksu@users.noreply.github.com>
diff --git a/plugins/woocommerce/includes/class-wc-customer.php b/plugins/woocommerce/includes/class-wc-customer.php
index 54cd5cdc649..1c9d2ea4aab 100644
--- a/plugins/woocommerce/includes/class-wc-customer.php
+++ b/plugins/woocommerce/includes/class-wc-customer.php
@@ -331,8 +331,10 @@ class WC_Customer extends WC_Legacy_Customer {
* considered optional and therefore valid, even if empty.
*/
foreach ( $shipping_address as $key => $value ) {
- // Skip further checks if the field has a value. From this point on $value is empty.
- if ( ! empty( $value ) ) {
+ // Skip further checks if the field has a value. From this point on it is blank or not
+ // an address. Must agree with OrderController::validate_address_fields(), which counts
+ // "0" as a value; a getter filter can return any type, and non-scalars are not an address.
+ if ( is_scalar( $value ) && '' !== trim( (string) $value ) ) {
continue;
}
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
index 2d5238f4363..a67947e8da9 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
@@ -350,6 +350,7 @@ class OrderController {
$this->validate_email( $order );
$this->validate_selected_shipping_methods( $needs_shipping, $chosen_shipping_methods );
$this->validate_addresses( $order, $needs_shipping );
+ $this->validate_shipping_was_not_skipped_for_missing_address( $needs_shipping );
// Perform custom validations.
$this->perform_custom_order_validation( $order );
@@ -1151,6 +1152,41 @@ class OrderController {
}
}
+ /**
+ * Check shipping was not withheld because "Hide shipping costs until an address is entered"
+ * judged the address incomplete.
+ *
+ * Without this, no packages are calculated, so validate_selected_shipping_methods() has nothing
+ * to reject and the order is placed with no shipping line. Mirrors the classic checkout's
+ * WC_Checkout::is_shipping_withheld_for_missing_address(), including its exemption for stores
+ * that switch shipping calculation off through the filter.
+ *
+ * @since 11.4.0
+ *
+ * @throws RouteException If shipping was withheld.
+ * @param bool $needs_shipping Whether the order needs shipping.
+ */
+ private function validate_shipping_was_not_skipped_for_missing_address( bool $needs_shipping ): void {
+ if ( ! $needs_shipping || 'yes' !== get_option( 'woocommerce_shipping_cost_requires_address' ) || wc()->cart->has_calculated_shipping() ) {
+ return;
+ }
+
+ /**
+ * This filter is documented in includes/class-wc-cart.php.
+ *
+ * @since 2.7.0
+ */
+ if ( (bool) apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true ) ) {
+ // No packages were calculated, so the checkout shows no shipping selector to pick from;
+ // say what the shopper can actually act on, as the classic checkout does.
+ throw new RouteException(
+ 'woocommerce_rest_invalid_shipping_option',
+ esc_html__( 'Please enter an address to continue.', 'woocommerce' ),
+ 400
+ );
+ }
+ }
+
/**
* Validate a given order key against an existing order.
*
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-customer-test.php b/plugins/woocommerce/tests/php/includes/class-wc-customer-test.php
index 8b3545e83fe..070350330f7 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-customer-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-customer-test.php
@@ -194,4 +194,43 @@ class WC_Customer_Test extends \WC_Unit_Test_Case {
return $customer;
}
+
+ /**
+ * Checkout validation counts "0" as a filled in field, so this check has to agree with it;
+ * a shopper whose town is literally "0" must not have shipping withheld.
+ *
+ * @testdox has_full_shipping_address() reads a field the way checkout validation does.
+ *
+ * @dataProvider provide_shipping_city_values
+ *
+ * @param mixed $city Shipping city to set.
+ * @param bool $expected Whether the address should read as complete.
+ */
+ public function test_has_full_shipping_address_agrees_with_checkout_validation( $city, bool $expected ): void {
+ $sut = new WC_Customer();
+ $sut->set_shipping_country( 'US' );
+ $sut->set_shipping_state( 'CA' );
+ $sut->set_shipping_city( $city );
+ $sut->set_shipping_postcode( '94103' );
+
+ $this->assertSame( $expected, $sut->has_full_shipping_address() );
+ }
+
+ /**
+ * Shipping city values, and whether each should leave the address complete. The non-string
+ * cases matter because a `woocommerce_customer_get_shipping_city` callback can return any type.
+ *
+ * @return array<string, array{0: mixed, 1: bool}>
+ */
+ public function provide_shipping_city_values(): array {
+ return array(
+ 'a town named zero' => array( '0', true ),
+ 'an ordinary town' => array( 'San Francisco', true ),
+ 'nothing typed' => array( '', false ),
+ 'only whitespace' => array( ' ', false ),
+ 'false from a filter' => array( false, false ),
+ 'null from a filter' => array( null, false ),
+ 'an array' => array( array(), false ),
+ );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
index 4306757022a..81fa66d446e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -2303,6 +2303,81 @@ class Checkout extends \WP_Test_REST_TestCase {
$this->assertEquals( 'Sorry, this order requires a shipping option.', $data['message'], print_r( $data, true ) );
}
+ /**
+ * A town of "0" is a value, so the cart must rate the address and the order must carry a
+ * shipping line. On trunk this same request stores an order with no shipping line at all.
+ * The guard is not exercised here, by design: once the address rates, it returns immediately.
+ *
+ * @testdox A shipping city of "0" is checked out with shipping charged.
+ */
+ public function test_checkout_charges_shipping_for_a_city_of_zero() {
+ $fixtures = new FixtureData();
+
+ // Load bearing: setUp() enables pickup, and with it on, show_shipping() returns before it
+ // ever looks at the address, so the cart would rate "0" on trunk too and this regression
+ // test would quietly stop testing anything.
+ $fixtures->shipping_remove_pickup_location();
+ update_option( 'woocommerce_shipping_cost_requires_address', 'yes' );
+
+ // setUp() adds the rate instance but leaves it free, and a free rate cannot show the
+ // difference between charging shipping and skipping it.
+ $zone_methods = \WC_Shipping_Zones::get_zone( 0 )->get_shipping_methods();
+ $flat_rate = reset( $zone_methods );
+
+ $flat_rate->instance_settings['cost'] = '25.00';
+ update_option( $flat_rate->get_instance_option_key(), $flat_rate->instance_settings );
+ \WC_Cache_Helper::get_transient_version( 'shipping', true );
+ WC()->shipping()->load_shipping_methods();
+
+ // Rate a complete address first, so a chosen method sits in the session the way the block
+ // checkout leaves it before the shopper submits.
+ wc()->customer->set_shipping_country( 'US' );
+ wc()->customer->set_shipping_state( 'CA' );
+ wc()->customer->set_shipping_city( 'San Francisco' );
+ wc()->customer->set_shipping_postcode( '94103' );
+ wc()->cart->calculate_totals();
+
+ $packages = wc()->shipping()->get_packages();
+ $rates = array_keys( $packages[0]['rates'] ?? array() );
+ $this->assertNotEmpty( $rates, 'The store must offer a rate, or this test proves nothing.' );
+ wc()->session->set( 'chosen_shipping_methods', array( $rates[0] ) );
+
+ wc()->session->set_customer_session_cookie( true );
+ wc()->session->save_data();
+
+ $address = array(
+ 'first_name' => 'Gina',
+ 'last_name' => 'Guest',
+ 'company' => '',
+ 'address_1' => '17 Private Lane',
+ 'address_2' => '',
+ 'city' => '0',
+ 'state' => 'CA',
+ 'postcode' => '94103',
+ 'country' => 'US',
+ 'email' => 'gina.guest@example.test',
+ 'phone' => '',
+ );
+
+ $request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
+ $request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
+ $request->set_body_params(
+ array(
+ 'billing_address' => $address,
+ 'shipping_address' => $address,
+ 'payment_method' => WC_Gateway_BACS::ID,
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $this->assertEquals( 200, $response->get_status(), print_r( $data, true ) );
+
+ $order = wc_get_order( $data['order_id'] );
+ $this->assertCount( 1, $order->get_items( 'shipping' ), 'A town of "0" must still be charged shipping.' );
+ $this->assertGreaterThan( 0, (float) $order->get_shipping_total(), 'The shipping total must not be zero.' );
+ }
+
/**
* Test deny guest checkout when registration during checkout is disabled,
* guest checkout is disabled and the user is not logged in.
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
index dc5e1b927e5..8772efcba45 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
@@ -115,6 +115,105 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
$this->assertNull( $this->sut->validate_selected_shipping_methods( false, array( 'free-shipping' ) ) );
}
+ /**
+ * The guard exists because an address the cart judges incomplete leaves no packages for
+ * validate_selected_shipping_methods() to inspect, so without it an order is placed with no
+ * shipping line. It mirrors WC_Checkout::is_shipping_withheld_for_missing_address().
+ *
+ * @testdox validate_order_before_payment() refuses an order whose shipping was withheld.
+ */
+ public function test_validate_order_before_payment_refuses_withheld_shipping(): void {
+ update_option( 'woocommerce_shipping_cost_requires_address', 'yes' );
+ $this->add_flat_rate_to_default_zone();
+ $this->add_product_to_cart_without_rating_shipping();
+
+ // The message, not just the type: validate_selected_shipping_methods() throws the same
+ // RouteException with the same error code, so only the wording tells the two apart.
+ $this->expectException( RouteException::class );
+ $this->expectExceptionMessage( 'Please enter an address to continue.' );
+ $this->sut->validate_order_before_payment( $this->create_order_with_full_shipping_address() );
+ }
+
+ /**
+ * The case that matters most in production: once the cart has rated an address, nothing may
+ * refuse the order, or every checkout on a store with the option enabled would fail.
+ *
+ * @testdox validate_order_before_payment() allows an order whose cart has rated the address.
+ */
+ public function test_validate_order_before_payment_allows_a_rated_cart(): void {
+ update_option( 'woocommerce_shipping_cost_requires_address', 'yes' );
+ $this->add_flat_rate_to_default_zone( '25.00' );
+
+ // A complete customer address, so the cart rates the package and picks the rate itself.
+ WC()->customer->set_shipping_city( 'San Francisco' );
+ WC()->customer->set_shipping_postcode( '94103' );
+
+ $product = WC_Helper_Product::create_simple_product();
+ WC()->cart->add_to_cart( $product->get_id(), 1 );
+ WC()->cart->calculate_totals();
+ $this->assertTrue(
+ WC()->cart->has_calculated_shipping(),
+ 'The cart must have rated the address, or this test proves nothing.'
+ );
+
+ $this->assertNull( $this->sut->validate_order_before_payment( $this->create_order_with_full_shipping_address() ) );
+ }
+
+ /**
+ * With the option off, shipping is never withheld for the address, so a cart with no rate is
+ * a store with no method configured, not a shopper slipping past one.
+ *
+ * @testdox validate_order_before_payment() allows an unrated cart when the option is off.
+ */
+ public function test_validate_order_before_payment_allows_an_unrated_cart_when_the_option_is_off(): void {
+ update_option( 'woocommerce_shipping_cost_requires_address', 'no' );
+
+ // Deliberately no shipping method: with the option off that is the only way a cart that
+ // needs shipping reaches checkout with nothing rated.
+ $this->add_product_to_cart_without_rating_shipping();
+
+ $this->assertNull( $this->sut->validate_order_before_payment( $this->create_order_with_full_shipping_address() ) );
+ }
+
+ /**
+ * @testdox validate_order_before_payment() allows a cart that does not need shipping.
+ */
+ public function test_validate_order_before_payment_allows_a_cart_that_does_not_need_shipping(): void {
+ update_option( 'woocommerce_shipping_cost_requires_address', 'yes' );
+ $this->add_flat_rate_to_default_zone();
+
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_virtual( true );
+ $product->save();
+ WC()->cart->add_to_cart( $product->get_id(), 1 );
+ $this->assertFalse( WC()->cart->needs_shipping() );
+
+ $this->assertNull( $this->sut->validate_order_before_payment( $this->create_order_with_full_shipping_address() ) );
+ }
+
+ /**
+ * A store that switches shipping calculation off through the filter withholds rates for its
+ * own reasons, so the order must go through, as it does on the classic checkout.
+ *
+ * @testdox validate_order_before_payment() allows an order when the filter stops calculation.
+ */
+ public function test_validate_order_before_payment_allows_shipping_calculation_disabled_by_filter(): void {
+ update_option( 'woocommerce_shipping_cost_requires_address', 'yes' );
+ $this->add_flat_rate_to_default_zone();
+
+ $filter = function () {
+ return false;
+ };
+ add_filter( 'woocommerce_cart_ready_to_calc_shipping', $filter );
+
+ try {
+ $this->add_product_to_cart_without_rating_shipping();
+ $this->assertNull( $this->sut->validate_order_before_payment( $this->create_order_with_full_shipping_address() ) );
+ } finally {
+ remove_filter( 'woocommerce_cart_ready_to_calc_shipping', $filter );
+ }
+ }
+
/**
* test_validate_order_before_payment_invalid_coupon_usage_limit.
*/
@@ -694,6 +793,55 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
return $order;
}
+ /**
+ * Add a flat rate to the catch-all zone, optionally with a cost so it actually offers a rate.
+ *
+ * @param string $cost Flat rate cost. The default leaves the rate off, as an unconfigured method is.
+ */
+ private function add_flat_rate_to_default_zone( string $cost = '' ): void {
+ $default_zone = \WC_Shipping_Zones::get_zone( 0 );
+ $default_zone->add_shipping_method( 'flat_rate' );
+ $default_zone->save();
+
+ if ( '' === $cost ) {
+ return;
+ }
+
+ $zone_methods = \WC_Shipping_Zones::get_zone( 0 )->get_shipping_methods();
+ $flat_rate = reset( $zone_methods );
+ $flat_rate->instance_settings['cost'] = $cost;
+ update_option( $flat_rate->get_instance_option_key(), $flat_rate->instance_settings );
+ \WC_Cache_Helper::get_transient_version( 'shipping', true );
+ WC()->shipping()->load_shipping_methods();
+ }
+
+ /**
+ * Put the cart in the state the bug produced: a shippable product, a method left in the
+ * session by an earlier request, and no package ever rated, so the shipping-method check has
+ * nothing to reject.
+ */
+ private function add_product_to_cart_without_rating_shipping(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ WC()->cart->add_to_cart( $product->get_id(), 1 );
+
+ $this->assertFalse( WC()->cart->has_calculated_shipping() );
+
+ // Any value will do: with no packages calculated, only its presence is checked.
+ WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate:1' ) );
+ }
+
+ /**
+ * An order carrying the complete address validate_addresses() asks for, so the shipping guard
+ * is the only thing left that can refuse it.
+ */
+ private function create_order_with_full_shipping_address(): \WC_Order {
+ $order = WC_Helper_Order::create_order();
+ $this->set_shipping_address( $order );
+ $order->save();
+
+ return $order;
+ }
+
/**
* @testdox Order locks are re-entrant within a request and released by their owner.
*/