ArrayUtil.php
1 year ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
10 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
NumberUtil.php
10 months ago
OrderUtil.php
8 months ago
PluginUtil.php
7 months ago
RestApiUtil.php
2 years ago
ShippingUtil.php
1 year ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
ShippingUtil.php
35 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ShippingUtil class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | namespace Automattic\WooCommerce\Utilities; |
| 8 | |
| 9 | /** |
| 10 | * The ShippingUtil class provides utilities for working with shipping and shipping packages. |
| 11 | */ |
| 12 | class ShippingUtil { |
| 13 | |
| 14 | /** |
| 15 | * Get the selected shipping rates from the packages. |
| 16 | * |
| 17 | * @param array $packages The packages to get the selected shipping rates from. |
| 18 | * @return \WC_Shipping_Rate[] The selected shipping rates. |
| 19 | */ |
| 20 | public static function get_selected_shipping_rates_from_packages( $packages ) { |
| 21 | return array_filter( |
| 22 | array_map( |
| 23 | function ( $package_id, $package ) { |
| 24 | $selected_rate_id = wc_get_chosen_shipping_method_for_package( $package_id, $package ); |
| 25 | $selected_rate = false !== $selected_rate_id && isset( $package['rates'][ $selected_rate_id ] ) ? $package['rates'][ $selected_rate_id ] : null; |
| 26 | |
| 27 | return $selected_rate instanceof \WC_Shipping_Rate ? $selected_rate : null; |
| 28 | }, |
| 29 | array_keys( $packages ), |
| 30 | array_values( $packages ) |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 |