问题
I am developing an eCommerce website on Wordpress using WooCommerce. I want to retrieve and display the 'Shipping Method' (i.e. Delivery or Collection etc.), that the customer had selected at the time of checkout, on the Order Confirmation page (i.e. after payment).
I am trying to do this using get_post_meta($post_id, $key, $single)
function. I am unable to do so as I don't know the $key value.
I tried the following code (within php tag):
echo get_post_meta( $order_id, 'shipping_method', true );
But it returns a blank value (no display on the page). I am assuming that I am using incorrect $key
.
I am open to suggestions to use some other (simpler) method also to achieve this.
回答1:
Since WooCommerce 3, if you want to get the shipping formatted method title(s), you can better use WC_Order
method get_shipping_method() like:
// Get the WC_Order object from the Order ID
$order = wc_get_order( $order_id );
// Output the shipping method(s) formatted method title(s)
echo $order->get_shipping_method();
For other shipping item details see the following threads:
- Get orders shipping method details in WooCommerce 3
- Show Shipping Method data on the Order edit pages in WooCommerce
来源:https://stackoverflow.com/questions/57444694/how-to-retrieve-shipping-method-of-an-order-in-woocommerce