Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the tax label per order item in Woocommerce 3

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

like image 840
Janine Kroser Avatar asked Sep 02 '25 11:09

Janine Kroser


1 Answers

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

like image 60
LoicTheAztec Avatar answered Sep 04 '25 00:09

LoicTheAztec