Commit d54562612a6 for woocommerce
commit d54562612a6b29403a93682daa37b981907333b5
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 12:38:44 2026 +0100
Enforce shipping countries when paying for Store API orders (#71196)
Store API: reject shipping countries the store doesn't ship to in pay for order
(cherry picked from commit 45314a8b3354223ec73c9fe8d2daadcecfb27edf)
Co-authored-by: Fernando Espinosa <fer@ferdev.com>
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
index 21ee48c5716..b72f307cdb7 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
@@ -9,7 +9,6 @@ use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use Automattic\WooCommerce\Utilities\ArrayUtil;
use Automattic\WooCommerce\Enums\OrderItemType;
use Automattic\WooCommerce\Utilities\DiscountsUtil;
-use Automattic\WooCommerce\Utilities\ShippingUtil;
use Exception;
/**
@@ -595,17 +594,35 @@ class OrderController {
$shipping_country = $order->get_shipping_country();
if ( $needs_shipping ) {
- $local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids();
- $selected_shipping_rates = ShippingUtil::get_selected_shipping_rates_from_packages( WC()->shipping()->get_packages() );
- $selected_shipping_rates_are_all_local_pickup = ArrayUtil::array_all(
- $selected_shipping_rates,
- function ( $rate ) use ( $local_pickup_method_ids ) {
- return in_array( $rate->get_method_id(), $local_pickup_method_ids, true );
+ // Read the shipping lines from the order, since the pay-for-order route never calculates the cart. An order
+ // with no shipping line is not local pickup, so its country is still checked.
+ // Use the canonical filter list as well as the registered methods: an existing order can carry a
+ // pickup method that is not registered in this request (e.g. pickup_location is only registered
+ // when the checkout block is the default, and legacy_local_pickup may be deregistered), and
+ // relying on registered methods alone would wrongly reject its shipping country.
+ // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Documented in WC_Abstract_Order::get_tax_location().
+ $filtered_local_pickup_ids = apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) );
+ $local_pickup_method_ids = array_values(
+ array_unique(
+ array_filter(
+ array_merge(
+ LocalPickupUtils::get_local_pickup_method_ids(),
+ is_array( $filtered_local_pickup_ids ) ? $filtered_local_pickup_ids : array()
+ ),
+ 'is_string'
+ )
+ )
+ );
+ $shipping_lines = $order->get_shipping_methods();
+ $shipping_is_all_local_pickup = ! empty( $shipping_lines ) && ArrayUtil::array_all(
+ $shipping_lines,
+ function ( $shipping_line ) use ( $local_pickup_method_ids ) {
+ return in_array( $shipping_line->get_method_id(), $local_pickup_method_ids, true );
}
);
// If only local pickup is selected, we don't need to validate the shipping country.
- if ( ! $selected_shipping_rates_are_all_local_pickup && ! $this->validate_allowed_country( $shipping_country, (array) wc()->countries->get_shipping_countries() ) ) {
+ if ( ! $shipping_is_all_local_pickup && ! $this->validate_allowed_country( $shipping_country, (array) wc()->countries->get_shipping_countries() ) ) {
$countries = WC()->countries->get_countries();
$shipping_country_name = $countries[ $shipping_country ] ?? $shipping_country;
throw new RouteException(
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 cd37da39d1f..4306757022a 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -2738,6 +2738,69 @@ class Checkout extends \WP_Test_REST_TestCase {
$this->assertEquals( $original_total, $stored_order->get_total() );
}
+ /**
+ * @testdox Existing order payment should check the shipping country against the order's shipping line, with an empty cart.
+ *
+ * @dataProvider provide_pay_for_order_shipping_methods
+ *
+ * @param string $method_id Method id of the order's shipping line.
+ * @param bool $expect_rejected Whether the shipping country should be rejected.
+ */
+ public function test_checkout_order_rejects_shipping_country_the_store_does_not_ship_to( string $method_id, bool $expect_rejected ) {
+ update_option( 'woocommerce_ship_to_countries', 'specific' );
+ update_option( 'woocommerce_specific_ship_to_countries', array( 'US' ) );
+ wc_empty_cart();
+
+ // A merchant-created order with no address yet, so the shopper fills in the first one.
+ $order = \WC_Helper_Order::create_order( 0 );
+ $order->set_billing_address( array_fill_keys( array( 'city', 'state', 'postcode', 'country' ), '' ) );
+ foreach ( $order->get_shipping_methods() as $shipping_line ) {
+ $shipping_line->set_method_id( $method_id );
+ $shipping_line->save();
+ }
+ $order->save();
+
+ $response = $this->dispatch_pay_for_order_request(
+ $order,
+ array(
+ 'city' => 'WooCity',
+ 'state' => 'NY',
+ 'postcode' => '12345',
+ 'country' => 'US',
+ ),
+ array(
+ 'address_1' => 'Hauptstr. 1',
+ 'city' => 'Berlin',
+ 'state' => '',
+ 'postcode' => '10115',
+ 'country' => 'DE',
+ )
+ );
+
+ $stored_order = wc_get_order( $order->get_id() );
+ if ( $expect_rejected ) {
+ $this->assertEquals( 400, $response->get_status() );
+ $this->assertEquals( 'woocommerce_rest_invalid_address_country', $response->get_data()['code'] );
+ $this->assertEquals( OrderStatus::PENDING, $stored_order->get_status() );
+ $this->assertEquals( '', $stored_order->get_shipping_country() );
+ } else {
+ $this->assertEquals( 200, $response->get_status(), wp_json_encode( $response->get_data() ) );
+ $this->assertEquals( 'DE', $stored_order->get_shipping_country() );
+ }
+ }
+
+ /**
+ * Shipping methods on a pay-for-order order and whether a country the store does not ship to should be rejected.
+ *
+ * @return array<string, array{0: string, 1: bool}>
+ */
+ public function provide_pay_for_order_shipping_methods(): array {
+ return array(
+ 'flat rate' => array( 'flat_rate', true ),
+ 'pickup location' => array( 'pickup_location', false ),
+ );
+ }
+
/**
* Returns a destination in a different shipping zone and tax location to the fixture's US address.
*
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 8e946dbc702..63bce0c9ae6 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
@@ -14,6 +14,16 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
* OrderControllerTests class.
*/
class OrderControllerTests extends \WC_Unit_Test_Case {
+ /**
+ * Shipping address fields in a country a US-only store does not ship to.
+ */
+ private const GERMAN_SHIPPING_ADDRESS = array(
+ 'country' => 'DE',
+ 'state' => '',
+ 'city' => 'Berlin',
+ 'postcode' => '10115',
+ );
+
/**
* The system under test.
*
@@ -280,17 +290,70 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
$order->set_shipping_country( 'Invalid' );
$order->save();
- // validate_addresses() inspects the selected shipping rates from the global cart's
- // packages even for existing orders, so the cart must contain the shippable product
- // for the shipping country check to run.
- /** @var \WC_Order_Item_Product $item */
- $array = $order->get_items();
- $item = reset( $array );
- $this->assertInstanceOf( \WC_Order_Item_Product::class, $item );
+ $this->sut->validate_existing_order_before_payment( $order );
+ }
- WC()->cart->add_to_cart( $item->get_product()->get_id() );
+ /**
+ * @testdox Existing-order validation decides local pickup from the order's shipping lines, not from the cart.
+ *
+ * @dataProvider provide_order_shipping_lines
+ *
+ * @param string[] $method_ids Method ids of the shipping lines on the order.
+ * @param bool $expect_rejected Whether the shipping country should be rejected.
+ */
+ public function test_validate_existing_order_shipping_country_uses_order_shipping_lines( array $method_ids, bool $expect_rejected ): void {
+ $order = $this->create_order_for_us_only_store( $method_ids, self::GERMAN_SHIPPING_ADDRESS );
- $this->sut->validate_existing_order_before_payment( $order );
+ if ( $expect_rejected ) {
+ $this->expectException( RouteException::class );
+ $this->expectExceptionMessage( 'Sorry, we do not ship orders to the provided country (Germany)' );
+ }
+
+ $this->assertNull( $this->sut->validate_existing_order_before_payment( $order ) );
+ }
+
+ /**
+ * Shipping lines on an order and whether a country the store does not ship to should be rejected.
+ *
+ * @return array<string, array{0: string[], 1: bool}>
+ */
+ public function provide_order_shipping_lines(): array {
+ return array(
+ 'flat rate' => array( array( 'flat_rate' ), true ),
+ 'no shipping line' => array( array(), true ),
+ 'local pickup and flat rate' => array( array( 'local_pickup', 'flat_rate' ), true ),
+ 'local pickup' => array( array( 'local_pickup' ), false ),
+ 'pickup location' => array( array( 'pickup_location' ), false ),
+ 'legacy local pickup' => array( array( 'legacy_local_pickup' ), false ),
+ );
+ }
+
+ /**
+ * @testdox Validating an existing order before an update rejects a shipping country the store does not ship to.
+ */
+ public function test_validate_existing_order_before_update_rejects_shipping_country_the_store_does_not_ship_to(): void {
+ $order = $this->create_order_for_us_only_store( array( 'flat_rate' ), self::GERMAN_SHIPPING_ADDRESS );
+
+ $this->expectException( RouteException::class );
+ $this->expectExceptionMessage( 'Sorry, we do not ship orders to the provided country (Germany)' );
+
+ $this->sut->validate_existing_order_before_update( $order );
+ }
+
+ /**
+ * @testdox Updating an existing order's addresses rejects a shipping country the store does not ship to.
+ */
+ public function test_update_existing_order_addresses_rejects_shipping_country_the_store_does_not_ship_to(): void {
+ $order = $this->create_order_for_us_only_store( array( 'flat_rate' ) );
+
+ $this->expectException( RouteException::class );
+ $this->expectExceptionMessage( 'Sorry, we do not ship orders to the provided country (Germany)' );
+
+ $this->sut->update_existing_order_addresses(
+ $order,
+ $order->get_address( 'billing' ),
+ array_merge( $order->get_address( 'shipping' ), self::GERMAN_SHIPPING_ADDRESS )
+ );
}
/**
@@ -590,6 +653,32 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
}
}
+ /**
+ * Creates a saved order that needs shipping, for a store that only ships to the US and has an empty cart.
+ *
+ * @param string[] $method_ids Method ids of the shipping lines on the order.
+ * @param array<string,string> $shipping_address Shipping address fields to override on the order.
+ * @return \WC_Order
+ */
+ private function create_order_for_us_only_store( array $method_ids, array $shipping_address = array() ): \WC_Order {
+ update_option( 'woocommerce_ship_to_countries', 'specific' );
+ update_option( 'woocommerce_specific_ship_to_countries', array( 'US' ) );
+ WC()->cart->empty_cart();
+
+ $order = WC_Helper_Order::create_order();
+ $order->remove_order_items( 'shipping' );
+ foreach ( $method_ids as $method_id ) {
+ $shipping_line = new \WC_Order_Item_Shipping();
+ $shipping_line->set_method_id( $method_id );
+ $order->add_item( $shipping_line );
+ }
+ $this->set_shipping_address( $order, $shipping_address );
+ $order->save();
+ $this->assertTrue( $order->needs_shipping(), 'The order must need shipping for the country check to apply.' );
+
+ return $order;
+ }
+
/**
* Helper method to set shipping address on an order.
*