Get orders shipping items details in WooCommerce 3(在 WooCommerce 3 中获取订单运送物品的详细信息)
问题描述
如何获取订单配送方式 ID.?
例如'flat_rate'.
For example 'flate_rate'.
自从 WooCommerce 3 以来,一切都发生了变化,现在变得很复杂.
Since WooCommerce 3 it is now complicated as everything has changed.
我已经在 foreach 循环中使用 $order->get_data() 进行了尝试,但数据受到保护.
I have tried it with $order->get_data() in a foreach loop but the data is protected.
推荐答案
如果您想获取 Order Items Shipping 数据,您需要先在 foreach 循环中获取它们(对于 'shipping' 项目类型)并使用 WC_Order_Item_Shipping 方法访问数据
If you want to get the Order Items Shipping data, you need first to get them in a foreach loop (for 'shipping' item type) and to use WC_Order_Item_Shipping methods to access data
$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();
}
您还可以使用 WC_Data 方法 get_data() 在这个 foreach 循环中:
You can also get an array of this (unprotected and accessible) data using the WC_Data method get_data() inside this foreach loop:
$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 与运输数据"相关的方法,例如以下示例:
To finish you can use the following WC_Abstract_Order methods related to "Shipping data", like in this examples:
// 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>';
这篇关于在 WooCommerce 3 中获取订单运送物品的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 WooCommerce 3 中获取订单运送物品的详细信息
基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
