Automattic\WooCommerce\StoreApi\Schemas\V1

CartShippingRateSchema::prepare_package_shipping_rates_response()protectedWC 1.0

Prepare an array of rates from a package for the response.

Method of the class: CartShippingRateSchema{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_package_shipping_rates_response( $package );
$package(array) (required)
Shipping package complete with rates from WooCommerce.

CartShippingRateSchema::prepare_package_shipping_rates_response() code WC 9.3.3

protected function prepare_package_shipping_rates_response( $package ) {
	$rates          = $package['rates'];
	$selected_rates = wc()->session->get( 'chosen_shipping_methods', array() );
	$selected_rate  = isset( $selected_rates[ $package['package_id'] ] ) ? $selected_rates[ $package['package_id'] ] : '';

	if ( empty( $selected_rate ) && ! empty( $package['rates'] ) ) {
		$selected_rate = wc_get_chosen_shipping_method_for_package( $package['package_id'], $package );
	}

	$response = [];

	foreach ( $package['rates'] as $rate ) {
		$response[] = $this->get_rate_response( $rate, $selected_rate );
	}

	return $response;
}