Automattic\WooCommerce\StoreApi\Utilities
OrderController::validate_selected_shipping_methods
Check there is a shipping method if it requires shipping.
Method of the class: OrderController{}
No Hooks.
Returns
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() OrderController::validate selected shipping methods code WC 9.9.3
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; } // Validate that the chosen shipping methods are valid according to the returned package rates. $packages = WC()->shipping()->get_packages(); foreach ( $packages as $package_id => $package ) { $chosen_rate_for_package = $chosen_shipping_methods[ $package_id ]; $valid_rate_ids_for_package = wp_list_pluck( $package['rates'], 'id' ); if ( ! is_string( $chosen_rate_for_package ) || ! ArrayUtils::string_contains_array( $chosen_rate_for_package, $valid_rate_ids_for_package ) ) { throw $exception; } } }