4

I'm trying to get the tax label in WooCommerce for each order item.

For example: 2x Product 1 - 19 % MwSt. (tax) 4x Product 2 - 19 % MwSt. (tax) 1x Product 2 - 19 % MwSt. (tax)

So I added the taxes "19 % (tax)" as standard value and "7 % (Tax)" as reduced tax in the woocommerce settings. I also set that I want to display and type-in prices in WooCommerce "without taxes".

Now I placed an order with these two items (digital and downloadable) and afterwards I try to get the tax values of each order line item.

$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('line_item')) as $item_id => $line_item) {

      $order_product_detail = $line_item->get_data();

      $item_tax_class = $order_product_detail['tax_class'];
      $item_subtotal_tax = $order_product_detail['subtotal_tax'];
      $item_total_tax = $order_product_detail['total_tax'];
      $item_taxes_array = $order_product_detail['taxes'];

      var_dump($item_tax_class);
      var_dump($item_subtotal_tax);
      var_dump($item_total_tax);
      var_dump($item_taxes_array);
}

This is the output:

string(0) "" string(4) "0.76" string(6) "0.2185" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.2185" } ["subtotal"]=> array(1) { [1]=> string(4) "0.76" } } string(0) "" string(4) "0.38" string(6) "0.2451" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.2451" } ["subtotal"]=> array(1) { [1]=> string(4) "0.38" } } string(0) "" string(4) "2.85" string(5) "2.166" array(2) { ["total"]=> array(1) { [1]=> string(5) "2.166" } ["subtotal"]=> array(1) { [1]=> string(4) "2.85" } } string(0) "" string(4) "0.76" string(6) "0.6251" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.6251" } ["subtotal"]=> array(1) { [1]=> string(4) "0.76" } } string(0) "" string(4) "0.95" string(6) "0.8151" array(2) { ["total"]=> array(1) { [1]=> string(6) "0.8151" } ["subtotal"]=> array(1) { [1]=> string(4) "0.95" } } string(0) "" string(4) "1.14" string(6) "1.0051" array(2) { ["total"]=> array(1) { [1]=> string(6) "1.0051" } ["subtotal"]=> array(1) { [1]=> string(4) "1.14" } } string(0) "" string(4) "1.52" string(6) "1.3851" array(2) { ["total"]=> array(1) { [1]=> string(6) "1.3851" } ["subtotal"]=> array(1) { [1]=> string(4) "1.52" } }

So as this doesn't give me the label of the tax per line item I tried the following:

$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('line_item')) as $item_id => $line_item) {
       $order_item_tax = new WC_Order_Item_Tax($item_id);
       var_dump($order_item_tax->get_label());
}

This is the output:

string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer"

But it should be "19 % MwSt.".

So I tried this one:

$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('tax')) as $item_id => $line_item) {
     var_dump($line_item->get_label());
}

This outputs:

string(10) "19 % MwSt."

So yeah, that's the value I need but why does it output this value only one time as I have seven items in the order?

Is there a chance to get the tax label of each order line item or isn't it possible in WooCommerce? My purpose is to create an invoice program but I have to consider if there are different tax rates per line.

WooCommerce Version is 3.4.2

Thanks in advance

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Janine Kroser
  • 444
  • 2
  • 6
  • 23

1 Answers1

6

The following code works since Woocommerce version 3.

It will allow you to get the tax label, as you can have many in an order depending on your tax settings, product settings and shipping settings.

// Get an instance of the WC_order object
$order = wc_get_order(143);

// Initializing variables
$tax_items_labels   = array(); // The tax labels by $rate Ids
$shipping_tax_label = '';      // The shipping tax label

// 1. Loop through order tax items
foreach ( $order->get_items('tax') as $tax_item ) {
    // Set the tax labels by rate ID in an array
    $tax_items_labels[$tax_item->get_rate_id()] = $tax_item->get_label();

    // Get the tax label used for shipping (if needed)
    if( ! empty($tax_item->get_shipping_tax_total()) )
        $shipping_tax_label = $tax_item->get_label();
}

// 2. Loop through order line items and get the tax label
foreach ( $order->get_items() as $item_id => $item ) {
    $taxes = $item->get_taxes();
    // Loop through taxes array to get the right label
    foreach( $taxes['subtotal'] as $rate_id => $tax ){
        $tax_label = $tax_items_labels[$rate_id]; // <== Here the line item tax label
        // Test output line items tax label
        echo '<pre>Item Id: '.$item_id.' | '; print_r($tax_label); echo '</pre>';
    }
}

// Test output shipping tax label
echo '<pre>Shipping tax label: '; print_r($shipping_tax_label); echo '</pre>';

Tested in Woocommerce version 3.4.2

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks, this indeed works correctly and WooCommerce uses a similar method in the order detail page, so i guess there's no easier way to do this. – passatgt May 25 '19 at 14:33
  • I worked on something similar and maybe it is useful to some people: cart.php template with an extra column display the line item's tax amount and rate: https://github.com/s-a-s-k-i-a/WooCommerce-Display-Tax-rate-and-amount-per-line-item-in-cart – Saskia Jul 20 '20 at 16:10
  • @Saskia Yes but hooks are a better way: 1) you don't need to update templates when WooCommerce update the related template. 2) Some themes already include templates customizations, so the necessary changes need to be made the theme template that you should copy before to a child theme… My answer code here just show a way to do it. – LoicTheAztec Jul 20 '20 at 16:38