5

I'm displaying my products in the cart without any taxes because I'm adding them afterwords to the subtotal sum in the cart. The problem is that I can't find a way to get the tax rate used for an order later on the My Orders page. So I'm looking for a way to get the tax rate used during the checkout for an order. The rate can be different depending on the country.

What I have is:

global $order;

$order->get_the_used_tax_rate_somehow();
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100

1 Answers1

7

To get the tax rate(s) from an order you will have to get order "tax" item(s).

You will get WC_Order_Item_Tax protected Object(s) and you have to use the dedicated available methods.

Sample code:

// Get an instance of the WC_Order Object
$order = wc_get_order($order_id);

// Loop through order tax items
foreach( $order->get_items('tax') as $item ){
    $name        = $item->get_name(); // Get rate code name (item title)
    $rate_code   = $item->get_rate_code(); // Get rate code
    $rate_label  = $item->get_label(); // Get label
    $rate_id     = $item->get_rate_id(); // Get rate Id
    $tax_total   = $item->get_tax_total(); // Get tax total amount (for this rate)
    $ship_total  = $item->get_shipping_tax_total(); // Get shipping tax total amount (for this rate)
    $is_compound = $item->is_compound(); // check if is compound (conditional)
    $compound    = $item->get_compound(); // Get compound
}

Note: An order can have multiple tax rates (items "tax").


You can also use some related WC_Abstract_Order methods on the WC_Order Object to get:

  • Get the tax location for the order: $order->get_tax_location() (array).
  • Get all tax classes for items in the order: $order->get_items_tax_classes() (array). It will display an empty value for the "Standard" tax class.
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your help Loic! That made it :) But now I've got another problem. Could you check if you already had this issue? https://stackoverflow.com/questions/57862170/how-can-i-get-all-available-tax-rates-in-woocommerce/57862400#57862400 – Mr. Jo Sep 10 '19 at 08:46