Automattic\WooCommerce\StoreApi\Utilities
OrderController::validate_location_change
Rejects a location change that would change the order's shipping cost or tax.
Method of the class: OrderController{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->validate_location_change( $order, $old_shipping_location, $old_tax_location, $needs_shipping ): void;
- $order(WC_Order) (required)
- Order with the new addresses set.
- $old_shipping_location(array) (required)
- Location the order shipped to before the change.
- $old_tax_location(array) (required)
- Location the order was taxed on before the change.
- $needs_shipping(true|false) (required)
- Whether the order needs shipping.
OrderController::validate_location_change() OrderController::validate location change code WC 11.3.0
private function validate_location_change( \WC_Order $order, array $old_shipping_location, array $old_tax_location, bool $needs_shipping ): void {
// An order with no address, e.g. one a merchant made for the shopper to fill in, is taxed at the shop's
// base address and has no shipping zone yet, so its first address is allowed. Store API orders get
// their address at checkout, so they never need this.
if ( empty( $old_shipping_location['country'] ) && 'store-api' !== $order->get_created_via() ) {
return;
}
if ( $needs_shipping && self::locations_differ( $old_shipping_location, self::get_shipping_location( $order ) ) ) {
throw new RouteException(
'woocommerce_rest_checkout_order_address_change_not_allowed',
esc_html__( 'Sorry, the shipping address on this order cannot be changed because the shipping cost was calculated for the original address. Please use the original address, or contact us to have the order updated.', 'woocommerce' ),
400
);
}
// get_taxable_location() follows whichever address sets the tax, including the shop base for local
// pickup and anything the woocommerce_order_get_tax_location filter returns. A store that collects
// no tax has no tax location to protect.
if ( wc_tax_enabled() && self::locations_differ( $old_tax_location, $order->get_taxable_location() ) ) {
throw new RouteException(
'woocommerce_rest_checkout_order_address_change_not_allowed',
esc_html__( 'Sorry, the address on this order cannot be changed because the tax was calculated for the original address. Please use the original address, or contact us to have the order updated.', 'woocommerce' ),
400
);
}
}