How to retrieve Shipping Method of an Order in WooCommerce?

末鹿安然 提交于 2021-01-20 04:52:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!