Automattic\WooCommerce\StoreApi\Utilities

OrderController::restore_state_names │ private │ WC 1.0

Keeps a state the order stores as a name, e.g. "New York", when the request sends its code, e.g. "NY".

This is because some edge cases would have an order with a state name (New York) instead of NY. Switching from New York to NY might trigger new totals so we avoid that by keeping the full name.

Method of the class: OrderController{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->restore_state_names( $order, $old_states ): void;
$order(WC_Order) (required)
Order with the new addresses set.
$old_states(string[]) (required)
State per address type before the new addresses were set.

OrderController::restore_state_names() code WC 11.3.0

private function restore_state_names( \WC_Order $order, array $old_states ): void {
	$validation_utils = new ValidationUtils();

	foreach ( $old_states as $address_type => $old_state ) {
		$state   = (string) $order->{"get_{$address_type}_state"}( 'edit' );
		$country = (string) $order->{"get_{$address_type}_country"}( 'edit' );

		if ( $old_state !== $state && $validation_utils->format_state( $old_state, $country ) === $state ) {
			$order->set_props( [ "{$address_type}_state" => $old_state ] );
		}
	}
}