WC_REST_Paypal_Standard_Controller::get_updated_shipping_options
Get the shipping options for the order.
Метод класса: WC_REST_Paypal_Standard_Controller{}
Хуков нет.
Возвращает
Массив. The shipping options.
Использование
// private - только в коде основоного (родительского) класса $result = $this->get_updated_shipping_options( $order, $selected_shipping_option );
- $order(WC_Order) (обязательный)
- The order object.
- $selected_shipping_option(массив) (обязательный)
- The selected shipping option.
Код WC_REST_Paypal_Standard_Controller::get_updated_shipping_options() WC REST Paypal Standard Controller::get updated shipping options WC 10.3.4
private function get_updated_shipping_options( $order, $selected_shipping_option ) {
WC()->cart->calculate_shipping();
$packages = WC()->shipping()->get_packages();
$order_shipping_rate_id = $this->get_order_shipping_rate_id( $order );
$has_selected_shipping_option = false;
$options = array();
foreach ( $packages as $package ) {
$rates = $package['rates'] ?? array();
foreach ( $rates as $rate ) {
if ( ! $rate instanceof \WC_Shipping_Rate ) {
continue;
}
$shipping_option_id = $rate->get_id();
// If a selected shipping option is sent in the request, check if it matches the shipping option id.
// Otherwise, if the order has a shipping method, check if the rate id matches the shipping option id.
if ( isset( $selected_shipping_option['id'] ) ) {
$is_selected = $shipping_option_id === $selected_shipping_option['id'];
} else {
$is_selected = $shipping_option_id === $order_shipping_rate_id;
}
if ( $is_selected ) {
$has_selected_shipping_option = true;
}
$options[] = array(
'id' => $shipping_option_id,
'type' => 'SHIPPING',
'amount' => array(
'currency_code' => $order->get_currency(),
'value' => wc_format_decimal( (float) $rate->get_cost(), wc_get_price_decimals() ),
),
'label' => $rate->get_label(),
'selected' => $is_selected,
);
}
}
// Set first option as selected if no option is selected.
if ( ! empty( $options ) && ! $has_selected_shipping_option ) {
$options[0]['selected'] = true;
}
return $options;
}