我怎样才能得到订单运输方式 ID.?
例如“flate_rate”。
自 WooCommerce 3 以来,一切都发生了变化,现在变得很复杂。
我已经尝试过$order->get_data()
在 foreach 循环中,但数据受到保护。
如果您想获取订单商品运输数据,您需要首先在 foreach 循环中获取它们(例如'shipping'
项目类型)和使用WC_订单_项目_运输 https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Shipping.html访问数据的方法
$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 https://docs.woocommerce.com/wc-apidocs/class-WC_Data.html#methods method get_data() https://docs.woocommerce.com/wc-apidocs/class-WC_Data.html#methods在这个 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 => $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 https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#methods与“运输数据”相关的方法,如本示例所示:
// 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>';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)