Получить информацию о способе доставки заказов в WooCommerce 3


Как я могу получить идентификатор способа доставки заказа ....

Например, "flate_rate".

Со времен WooCommerce 3.x.x это не так просто, так как все изменилось.

Я попробовал это с $order->get_data() в цикле foreach, но данные защищены.

Author: LoicTheAztec, 2017-09-07

1 answers

Если вы хотите получить данные о доставке товаров для заказа, вам сначала нужно получить их в цикле foreach (для 'shipping' тип элемента) и использовать методы wc_order_item_shipping для доступа к данным

$order_id = 528; // For example

// An instance of 
$order = wc_get_order($order_id);

// Iterating through order shipping items
foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
    $order_item_name           = $shipping_item_obj->get_name();
    $order_item_type           = $shipping_item_obj->get_type();
    $shipping_method_title     = $shipping_item_obj->get_method_title();
    $shipping_method_id        = $shipping_item_obj->get_method_id(); // The method ID
    $shipping_method_total     = $shipping_item_obj->get_total);
    $shipping_method_total_tax = $shipping_item_obj->get_total_tax();
    $shipping_method_taxes     = $shipping_item_obj->get_taxes();
}

Вы также можете получить массив этих (незащищенных и доступных) данных с помощью WC_Data способ get_data() внутри этого цикла foreach:

$order_id = 528; // For example

// An instance of 
$order = wc_get_order($order_id);

// Iterating through order shipping items
foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
    // Get the data in an unprotected array
    $shipping_item_data = $shipping_item_obj->get_data();

    $shipping_data_id           = $shipping_data['id'];
    $shipping_data_order_id     = $shipping_data['order_id'];
    $shipping_data_name         = $shipping_data['name'];
    $shipping_data_method_title = $shipping_data['method_title'];
    $shipping_data_method_id    = $shipping_data['method_id'];
    $shipping_data_total        = $shipping_data['total'];
    $shipping_data_total_tax    = $shipping_data['total_tax'];
    $shipping_data_taxes        = $shipping_data['taxes'];
}

Для завершения вы можете использовать следующее WC_Abstract_Order методы, связанные к "Данным о доставке", как в этом примере:

// Get an instance of the WC_Order object
$order = wc_get_order(522);

// Return an array of shipping costs within this order.
$order->get_shipping_methods(); 

// Conditional function based on the Order shipping method 
if( $order->has_shipping_method('flat_rate') ) { 

    // Output formatted shipping method title.
    echo '<p>Shipping method name: '. $order->get_shipping_method()) .'</p>';
 8
Author: LoicTheAztec, 2017-09-08 02:11:15