6

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.

M. Khan
  • 105
  • 2
  • 7

1 Answers1

7

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:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks. Your suggested method works well and I will go with that. I got an error using implode() function; so just ignored it. One more thing, can I use WC_Order method to retrieve value of a custom field also in the checkout (which I added additionally to the checkout form)? – M. Khan Aug 10 '19 at 19:23
  • Even the updated code (in the first option) doesnt work. I tried a few things to check the command flow. It does execute the "echo" command in "else" block but there is no value available in $shipping_method to display. This indicates that the get_post_meta() function is not capturing any value in $shipping_method, probably because the key (_shipping_method) is not the right one. – M. Khan Aug 10 '19 at 21:32
  • @M.Khan So use the second method which is the best since WooCommerce 3. – LoicTheAztec Aug 10 '19 at 22:23