在 WooCommerce 的订单编辑页面上显示运输方式数据 [英] Show Shipping Method data on the Order edit pages in WooCommerce
问题描述
在 本网站上的 WooCommerce 中,我有 2 种本地取货运输方式:
In WooCommerce on this website, I have 2 Local Pickup shipping methods:
- 上午取件:
shipping_method_0_local_pickup31
- 下午取件:
shipping_method_0_local_pickup32
很遗憾,此送货方式未显示在管理订单编辑页面上
Unfortunately this shipping method does not show up on Admin Order edit pages
可以使用:add_action('woocommerce_admin_order_data_after_shipping_address','cwn_add_pickup_to_order_item_meta', 1, 2);
Is is possible to use: add_action('woocommerce_admin_order_data_after_shipping_address','cwn_add_pickup_to_order_item_meta', 1, 2);
function cwn_add_pickup_to_order_item_meta($shipping_method) {
echo "<h4>Pickup Time</h4>";
echo '<p><strong>' . __('AM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup31', true) . '</p>';
echo '<p><strong>' . __('PM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup32', true) . '</p>';
}
推荐答案
在 WC_Order 对象中有 2 种类似的方法可以获取 Shipping methods 数据.
There is 2 similar ways to get the Shipping methods data in the WC_Order object.
1) 使用 WC_Abstract_Order
get_shipping_methods()
方法.2) 使用 WC_Abstract_Order
get_items('shipping')
方法.
1) Using WC_Abstract_Order
get_shipping_methods()
method.
2) Using WC_Abstract_Order
get_items( 'shipping' )
method.
这将输出一组 WC_Order_Item_Shipping代码>
对象.所以你需要一个 foreach 循环来使用 WC_Order_Item_Shipping
方法来获取数据,这样(Where $order
是 WC_Order
对象的实例):
This will output an array of WC_Order_Item_Shipping
objects. So you will need a foreach loop to use WC_Order_Item_Shipping
methods to get the data, this way (Where $order
is an instance of the WC_Order
object):
foreach($order->get_items( 'shipping' ) as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
//Or:
foreach($order->get_shipping_methods() as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
<块引用>
在您的代码中,$shipping_method
挂钩函数参数是错误的,因为它应该是 $order
(WC_Order 对象
的一个实例.
所以现在你可以在你的钩子函数中使用它,例如:
In your code, the
$shipping_method
hooked function argument is wrong as it should be$order
(an instance ofWC_Order object
.
So now you can use it in your hooked function, this way for example:
<strong>运输方式名称:</strong>'.$shipping_method->get_name().'
<strong>发货方式标题:</strong>'.$shipping_method->get_method_title().'
<strong>运输方式总计:</strong>'.$shipping_method->get_total().'
<strong>运输方式 总税款:</strong>'.$shipping_method->get_total_tax().'</p><br>';}回声'</div>';}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'cwn_add_pickup_to_order_item_meta', 10, 1 );
function cwn_add_pickup_to_order_item_meta( $order ){
echo '<div>';
foreach($order->get_shipping_methods() as $shipping_method ){
echo '<p><strong>Shipping Method ID:</strong> '. $shipping_method->get_method_id().'<br>
<strong>Shipping Method name:</strong> '. $shipping_method->get_name().'<br>
<strong>Shipping Method title:</strong> '. $shipping_method->get_method_title().'<br>
<strong>Shipping Method Total:</strong> '. $shipping_method->get_total().'<br>
<strong>Shipping Method Total tax:</strong> '. $shipping_method->get_total_tax().'</p><br>';
}
echo '</div>';
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中
在 WooCommerce 3+ 上测试和工作
Tested and works on WooCommerce 3+
这篇关于在 WooCommerce 的订单编辑页面上显示运输方式数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!