Automattic\WooCommerce\StoreApi\Utilities

OrderController::validate_selected_shipping_methods()publicWC 1.0

Check there is a shipping method if it requires shipping.

Method of the class: OrderController{}

No Hooks.

Return

null. Nothing (null).

Usage

$OrderController = new OrderController();
$OrderController->validate_selected_shipping_methods( $needs_shipping, $chosen_shipping_methods );
$needs_shipping(true|false) (required)
Current order needs shipping.
$chosen_shipping_methods(array)
Array of shipping methods.
Default: array()

OrderController::validate_selected_shipping_methods() code WC 8.9.0

public function validate_selected_shipping_methods( $needs_shipping, $chosen_shipping_methods = array() ) {
	if ( ! $needs_shipping ) {
		return;
	}

	$exception = new RouteException(
		'woocommerce_rest_invalid_shipping_option',
		__( 'Sorry, this order requires a shipping option.', 'woocommerce' ),
		400,
		array()
	);

	if ( ! is_array( $chosen_shipping_methods ) || empty( $chosen_shipping_methods ) ) {
		throw $exception;
	}

	$valid_methods = array_keys( WC()->shipping()->get_shipping_methods() );

	foreach ( $chosen_shipping_methods as $chosen_shipping_method ) {
		if (
			false === $chosen_shipping_method ||
			! is_string( $chosen_shipping_method ) ||
			! ArrayUtils::string_contains_array( $chosen_shipping_method, $valid_methods )
		) {
			throw $exception;
		}
	}
}