OnurK.
OnurK.

Reputation: 121

Get separate Order's tax totals amounts with tax class

I'm making personal a PDF invoice plugin for woocommerce.

Some of our products has tax with Reduced rate (%8) or Standart rate (%18). For example, Product A = Reduced rate (%8), Product B = Standart rate (%18). I can get total of tax amount easily but I want to print with sperate tax total amounts with tax class.

How to get total Reduced rate tax amount of an order? Also Standart rate.

How can I echo them separately?

Upvotes: 2

Views: 1019

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The Tax class is not registered anywhere in the order data… It's registered in each product and optionally for shipping.

You can simply use the dedicated WC_Abstract_Order method get_tax_totals() (as Woocommerce uses for separated tax rows) and you will have the tax label percentage that is set in each tax line settings.

The Rate code $rate_code is made up of COUNTRY-STATE-NAME-Priority.
For example: GB-VAT-1 or US-AL-TAX-1.

The code to display separated tax rows:

// Get the WC_Order instance Object from the Order ID (if needed)
$order = wc_get_order($order_id);

// Output the tax rows in a table
echo '<table>';
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
    $tax_rate_id  = $tax->rate_id;
    $tax_label    = $tax->label;
    $tax_amount   = $tax->amount;
    $tax_f_amount = $tax->formatted_amount;
    $compound     = $tax->is_compound;
    echo '<tr><td>' . $tax_label  . ': </td><td>' . $tax_f_amount . '</td></tr>';
}
echo '</table>';

If you want to display something like Reduced rate (%8) or Standart rate (%18), you will have to customize the "Tax name" in the Tax settings for each tax line in each Tax rate (But it will be displayed everywhere and not only in your custom PDF plugin).

Additionally, the Tax class is just for settings purpose and for admin view.

Upvotes: 2

Related Questions