Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the order total shipping in Woocommerce 3

I'm trying to get the shipping method or cost of shipping for a WooCommerce order - I'm writing a custom email template that is different depending on free delivery vs paid delivery.

I found a function called get_total_shipping(), but this is now deprecated and I cannot find a replacement - does one exist?

I've noticed that the shipping amount is stored in a hidden meta field (_order_shipping), which I can access, but I worry that this might break on future WooCommerce updates.

like image 918
user319940 Avatar asked Dec 24 '22 07:12

user319940


1 Answers

Since Woocommerce 3 get_total_shipping() method is replaced by get_shipping_total() .

So there is actually 2 available CRUD getters methods for shipping totals in WC_Abstract_Order Class that can be used on the WC_Order instance object:

  • get_shipping_total() that is the shipping total excluding taxes
  • get_shipping_tax() that is the shipping taxes total

So you will use them with the $order variable object simply this way:

$shipping_total = $order->get_shipping_total();
$shipping_tax   = $order->get_shipping_tax();

There is also get_shipping_to_display() method that will output the formatted shipping total.

like image 137
LoicTheAztec Avatar answered Jan 08 '23 21:01

LoicTheAztec