php - 在 WooCommerce 3 中获取订单发货项目详细信息

标签 php woocommerce orders shipping shipping-method

如何获取订单送货方式 ID

例如“flate_rate”。

自 WooCommerce 3 以来,一切都变了,现在变得复杂了。

我已经在 foreach 循环中使用 $order->get_data() 进行了尝试,但数据受到保护。

最佳答案

如果您想获取订单商品运输数据,您首先需要在 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 => $item ){
    $order_item_name             = $item->get_name();
    $order_item_type             = $item->get_type();
    $shipping_method_title       = $item->get_method_title();
    $shipping_method_id          = $item->get_method_id(); // The method ID
    $shipping_method_instance_id = $item->get_instance_id(); // The instance ID
    $shipping_method_total       = $item->get_total();
    $shipping_method_total_tax   = $item->get_total_tax();
    $shipping_method_taxes       = $item->get_taxes();
}

您还可以使用此 foreach 循环中的 WC_Data 方法 get_data() 获取此(未 protected 且可访问的)数据的数组:

$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 => $item ){
    // Get the data in an unprotected array
    $item_data = $item->get_data();

    $shipping_data_id           = $item_data['id'];
    $shipping_data_order_id     = $item_data['order_id'];
    $shipping_data_name         = $item_data['name'];
    $shipping_data_method_title = $item_data['method_title'];
    $shipping_data_method_id    = $item_data['method_id'];
    $shipping_data_instance_id  = $item_data['instance_id'];
    $shipping_data_total        = $item_data['total'];
    $shipping_data_total_tax    = $item_data['total_tax'];
    $shipping_data_taxes        = $item_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(); // same thing than $order->get_items('shipping')

// 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>';

关于php - 在 WooCommerce 3 中获取订单发货项目详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46102428/

相关文章:

php - 社交网络墙发布逻辑不起作用

php - 无法使用PHPunit Selenium执行javascript

php - WooCommerce:使用 SQL 查询获取产品类别的购买商品

OpenCart 3 订单状态作为缺失状态出现

php - 在批量订单面板上删除或隐藏 woocommerce 上的 "add new"按钮

php - 在管理订单中显示 Woocommerce 自定义结账字段值,使其可编辑

php - 连接 MySQL 时出现访问被拒绝错误

PHP session 添加 header Pragma : no-cache

mysql - "Insert if doesn' t 在 post_meta 表 WP 中存在 "only for product type = ' 产品

mysql - 获取 Woocommerce 客户订单语言