WooCommerce Code Reference

ShippingUtil
in package

The ShippingUtil class provides utilities for working with shipping and shipping packages.

Table of Contents

$cart_package_keys  : array{state: string, keys: array|null}|null
The package keys of the cart from the last build, with the cart state they belong to.
filter_chosen_shipping_methods()  : array<string|int, mixed>
Keep only the chosen methods whose rates were listed for the current cart packages.
get_selected_shipping_rates_from_packages()  : array<string|int, WC_Shipping_Rate>
Get the selected shipping rates from the packages.
is_shipping_rate_listed()  : bool
Check that a rate was listed for a package key the last time shipping was calculated.

Properties

$cart_package_keys

The package keys of the cart from the last build, with the cart state they belong to.

private static array{state: string, keys: array|null}|null $cart_package_keys = null

Methods

filter_chosen_shipping_methods()

Keep only the chosen methods whose rates were listed for the current cart packages.

public static filter_chosen_shipping_methods(mixed $chosen_methods) : array<string|int, mixed>

Each rate must be listed for its key. Key must be a shipping package of the cart at the time of request. The session can keep keys from earlier requests and keys that other code adds for carts other than this one.

Parameters
$chosen_methods : mixed

Chosen methods keyed by package key, usually the chosen_shipping_methods session value.

Tags
since
11.2.2
Return values
array<string|int, mixed> — The listed entries with their keys kept. Empty when the input is not an array.

get_selected_shipping_rates_from_packages()

Get the selected shipping rates from the packages.

public static get_selected_shipping_rates_from_packages(array<string|int, mixed> $packages) : array<string|int, WC_Shipping_Rate>
Parameters
$packages : array<string|int, mixed>

The packages to get the selected shipping rates from.

Return values
array<string|int, WC_Shipping_Rate> — The selected shipping rates.

is_shipping_rate_listed()

Check that a rate was listed for a package key the last time shipping was calculated.

public static is_shipping_rate_listed(int|string $package_key, mixed $rate_id) : bool

A rate id in the chosen_shipping_methods session value is used only when the previous_shipping_methods session value lists it for the same key.

Parameters
$package_key : int|string

Package key, as used in chosen_shipping_methods.

$rate_id : mixed

Rate id to check. Only strings can match.

Tags
since
11.2.2
Return values
bool —