Mr. Jo
Mr. Jo

Reputation: 5251

Get the tax rate(s) from an order in WooCommerce 3

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();

Upvotes: 5

Views: 12594

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

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.

Upvotes: 7

Related Questions