在 WooCommerce 的订单编辑页面上显示送货方式数据
Show Shipping Method data on the Order edit pages in WooCommerce
在 this website 上的 WooCommerce 中,我有 2 种本地取货送货方式:
- 上午取件:
shipping_method_0_local_pickup31
- 下午取件:
shipping_method_0_local_pickup32
遗憾的是,此送货方式未显示在管理员订单编辑页面上
是否可以使用:
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 数据。
1) 使用WC_Abstract_Order
get_shipping_methods()
方法。
2) 使用WC_Abstract_Order
get_items( 'shipping' )
方法。
这将输出 WC_Order_Item_Shipping
个对象的数组。所以你需要一个foreach循环来使用WC_Order_Item_Shipping
方法来获取数据,这样(其中$order
是 WC_Order
对象的实例):
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>';
}
In your code, the $shipping_method
hooked function argument is wrong as it should be $order
(an instance of WC_Order object
.
所以现在你可以在你的钩子函数中使用它,例如这样:
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+
我正在寻找一种在完成购买时执行命令的方法,但选择的送货方式与“本地取件".
你最后的代码给了我需要的光。
我的代码有效。
按照我的代码来帮助任何需要它的人:
function showpopup($order_id){
$order = wc_get_order($order_id);
$order->get_shipping_methods();
if( ! $order->has_shipping_method('local_pickup') ) {
echo '
<script>
function myFunction() {
alert("Retirada no local NÃO SELECIONADA!");
}
myFunction()
</script>
';
}
}
add_action( 'woocommerce_thankyou', 'showpopup', 10, 1 );
谢谢。
在 this website 上的 WooCommerce 中,我有 2 种本地取货送货方式:
- 上午取件:
shipping_method_0_local_pickup31
- 下午取件:
shipping_method_0_local_pickup32
遗憾的是,此送货方式未显示在管理员订单编辑页面上
是否可以使用: 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 数据。
1) 使用WC_Abstract_Order
get_shipping_methods()
方法。
2) 使用WC_Abstract_Order
get_items( 'shipping' )
方法。
这将输出 WC_Order_Item_Shipping
个对象的数组。所以你需要一个foreach循环来使用WC_Order_Item_Shipping
方法来获取数据,这样(其中$order
是 WC_Order
对象的实例):
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>';
}
In your code, the
$shipping_method
hooked function argument is wrong as it should be$order
(an instance ofWC_Order object
.
所以现在你可以在你的钩子函数中使用它,例如这样:
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+
我正在寻找一种在完成购买时执行命令的方法,但选择的送货方式与“本地取件".
你最后的代码给了我需要的光。 我的代码有效。
按照我的代码来帮助任何需要它的人:
function showpopup($order_id){
$order = wc_get_order($order_id);
$order->get_shipping_methods();
if( ! $order->has_shipping_method('local_pickup') ) {
echo '
<script>
function myFunction() {
alert("Retirada no local NÃO SELECIONADA!");
}
myFunction()
</script>
';
}
}
add_action( 'woocommerce_thankyou', 'showpopup', 10, 1 );
谢谢。