Get separate Order's tax totals amounts with tax class

冷暖自知 提交于 2021-02-07 09:29:54

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/52957544/get-separate-orders-tax-totals-amounts-with-tax-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!