Automattic\WooCommerce\StoreApi\Utilities

OrderController::validate_addressesprotectedWC 1.0

Validates customer address data based on the locale to ensure required fields are set.

Method of the class: OrderController{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_addresses( $order, $needs_shipping );
$order(WC_Order) (required)
Order object.
$needs_shipping(true|false) (required)
Whether the order needs shipping.

OrderController::validate_addresses() code WC 9.9.4

protected function validate_addresses( \WC_Order $order, bool $needs_shipping ) {
	$errors           = new \WP_Error();
	$billing_country  = $order->get_billing_country();
	$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 );
			}
		);

		// 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() ) ) {
			throw new RouteException(
				'woocommerce_rest_invalid_address_country',
				sprintf(
					/* translators: %s country code. */
					esc_html__( 'Sorry, we do not ship orders to the provided country (%s)', 'woocommerce' ),
					esc_html( $shipping_country )
				),
				400,
				array(
					'allowed_countries' => array_map( 'esc_html', array_keys( wc()->countries->get_shipping_countries() ) ),
				)
			);
		}
	}

	if ( ! $this->validate_allowed_country( $billing_country, (array) wc()->countries->get_allowed_countries() ) ) {
		throw new RouteException(
			'woocommerce_rest_invalid_address_country',
			sprintf(
				/* translators: %s country code. */
				esc_html__( 'Sorry, we do not allow orders from the provided country (%s)', 'woocommerce' ),
				esc_html( $billing_country )
			),
			400,
			array(
				'allowed_countries' => array_map( 'esc_html', array_keys( wc()->countries->get_allowed_countries() ) ),
			)
		);
	}

	if ( $needs_shipping ) {
		$this->validate_address_fields( $order, 'shipping', $errors );
	}
	$this->validate_address_fields( $order, 'billing', $errors );

	if ( ! $errors->has_errors() ) {
		return;
	}

	$errors_by_code = array();
	$error_codes    = $errors->get_error_codes();

	foreach ( $error_codes as $code ) {
		$errors_by_code[ $code ] = $errors->get_error_messages( $code );
	}

	// Surface errors from first code.
	foreach ( $errors_by_code as $code => $error_messages ) {
		throw new RouteException(
			'woocommerce_rest_invalid_address',
			sprintf(
				/* translators: %s Address type. */
				esc_html__( 'There was a problem with the provided %s:', 'woocommerce' ) . ' ' . esc_html( implode( ', ', $error_messages ) ),
				'shipping' === $code ? esc_html__( 'shipping address', 'woocommerce' ) : esc_html__( 'billing address', 'woocommerce' )
			),
			400,
			array(
				'errors' => $errors_by_code, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
			)
		);
	}
}