Automattic\WooCommerce\Utilities
ShippingUtil::filter_chosen_shipping_methods
Keep only the chosen methods whose rates were listed for the current cart packages.
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.
Метод класса: ShippingUtil{}
Хуки из метода
Возвращает
array. The listed entries with their keys kept. Empty when the input is not an array.
Использование
$result = ShippingUtil::filter_chosen_shipping_methods( $chosen_methods ): array;
- $chosen_methods(разное) (обязательный)
- Chosen methods keyed by package key, usually the
chosen_shipping_methodssession value.
Список изменений
| С версии 11.2.2 | Введена. |
Код ShippingUtil::filter_chosen_shipping_methods() ShippingUtil::filter chosen shipping methods WC 11.2.2
public static function filter_chosen_shipping_methods( $chosen_methods ): array {
if ( ! is_array( $chosen_methods ) || ! is_callable( array( WC()->session, 'get' ) ) ) {
return array();
}
$listed_rates = WC()->session->get( 'previous_shipping_methods' );
$listed_rates = is_array( $listed_rates ) ? $listed_rates : array();
$chosen_methods = array_filter(
$chosen_methods,
function ( $rate_id, $package_key ) use ( $listed_rates ) {
$rate_ids = $listed_rates[ $package_key ] ?? null;
// Compare as strings: array_keys() turns a numeric rate id into an int.
return is_string( $rate_id ) && is_array( $rate_ids ) && in_array( $rate_id, array_map( 'strval', array_filter( $rate_ids, 'is_scalar' ) ), true );
},
ARRAY_FILTER_USE_BOTH
);
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Documented in WC_Abstract_Order::get_tax_location().
$pickup_method_ids = apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) );
$pickup_method_ids = is_array( $pickup_method_ids ) ? $pickup_method_ids : array();
$package_keys = null;
foreach ( $chosen_methods as $package_key => $rate_id ) {
if ( ! in_array( explode( ':', $rate_id )[0], $pickup_method_ids, true ) ) {
continue;
}
// This runs on every tax rate lookup, so build the keys again only when the cart state changes.
// The totals counters change for each Subscriptions recurring cart, which uses other keys.
if ( null === $package_keys ) {
$cart = WC()->cart;
if ( ! $cart instanceof \WC_Cart ) {
$package_keys = array();
} else {
$customer = WC()->customer;
$state = md5(
(string) wp_json_encode(
array(
spl_object_id( $cart ),
wp_list_pluck( $cart->get_cart_contents(), 'quantity' ),
$customer instanceof \WC_Customer ? array(
$customer->get_shipping_country(),
$customer->get_shipping_state(),
$customer->get_shipping_postcode(),
$customer->get_shipping_city(),
$customer->get_shipping_address_1(),
$customer->get_shipping_address_2(),
) : array(),
did_action( 'woocommerce_before_calculate_totals' ),
did_action( 'woocommerce_after_calculate_totals' ),
)
)
);
if ( null === self::$cart_package_keys || self::$cart_package_keys['state'] !== $state ) {
// Save the state first: a nested call from the package filter sees null keys, keeps the pickup and does not build the packages again.
self::$cart_package_keys = array(
'state' => $state,
'keys' => null,
);
try {
self::$cart_package_keys['keys'] = array_fill_keys( array_keys( $cart->get_shipping_packages() ), true );
} finally {
// A build that did not complete leaves the keys for the current state null.
if ( null === ( self::$cart_package_keys['keys'] ?? null ) ) {
self::$cart_package_keys = null;
}
}
}
$package_keys = self::$cart_package_keys['keys'] ?? null;
if ( null === $package_keys ) {
break;
}
}
}
if ( ! isset( $package_keys[ $package_key ] ) ) {
unset( $chosen_methods[ $package_key ] );
}
}
return $chosen_methods;
}