Automattic\WooCommerce\StoreApi\Utilities

OrderController::update_existing_order_addresses │ public │ WC 11.3.0

Sets new addresses on an existing order and validates them, without saving the order.

Rejects a location change that would change the shipping cost or tax. The request recalculates tax but not shipping, and the total should stay what the order was priced at.

Method of the class: OrderController{}

No Hooks.

Returns

array|null. Shipping address set on the order, or null if the order kept its own.

Usage

$OrderController = new OrderController();
$OrderController->update_existing_order_addresses( $order, $billing, ?array $shipping ): ?array;
$order(WC_Order) (required)
Existing order.
$billing(array) (required)
New billing address.
?array $shipping(required)
.

Changelog

Since 11.3.0 Introduced.

OrderController::update_existing_order_addresses() code WC 11.3.0

public function update_existing_order_addresses( \WC_Order $order, array $billing, ?array $shipping ): ?array {
	$old_shipping_location = self::get_shipping_location( $order );
	$old_tax_location      = $order->get_taxable_location();
	$old_states            = [
		'billing'  => $order->get_billing_state( 'edit' ),
		// An order with no shipping address ships to billing, so its shipping starts from the billing state.
		'shipping' => $order->get_shipping_country() ? $order->get_shipping_state( 'edit' ) : $order->get_billing_state( 'edit' ),
	];

	if ( wc_ship_to_billing_address_only() ) {
		// Stores that ship to billing only ignore the request's shipping. An order that already ships somewhere
		// else, e.g. one made in the admin, keeps its shipping, since copying billing over it would block payment.
		$shipping = self::locations_differ( $old_shipping_location, $order->get_address( 'billing' ) ) ? null : $billing;
	} elseif ( null === $shipping && ! $order->get_shipping_country() ) {
		// An order with no shipping address to keep ships to billing.
		$shipping = $billing;
	}

	$order->set_billing_address( $billing );
	if ( null !== $shipping ) {
		$order->set_shipping_address( $shipping );
	}
	$this->restore_state_names( $order, $old_states );

	$needs_shipping = $order->needs_shipping();
	$this->validate_addresses( $order, $needs_shipping );
	$this->validate_location_change( $order, $old_shipping_location, $old_tax_location, $needs_shipping );

	return $shipping;
}