问题
In WooCommerce on this website, I have 2 Local Pickup shipping methods:
- AM pickup:
shipping_method_0_local_pickup31
- PM pickup:
shipping_method_0_local_pickup32
Unfortunately this shipping method does not show up on Admin Order edit pages
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>';
}
回答1:
There is 2 similar ways to get the Shipping methods data in the WC_Order object.
1) Using WC_Abstract_Order
get_shipping_methods() method.
2) Using WC_Abstract_Order
get_items( 'shipping' ) method.
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>';
}
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:
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>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file
Tested and works on WooCommerce 3+
来源:https://stackoverflow.com/questions/47133027/show-shipping-method-data-on-the-order-edit-pages-in-woocommerce