如何在WooCommerce中获取订单税详细信息和费率?

时间:2020-06-09 11:21:28

标签: php wordpress woocommerce orders tax

我正在尝试从订单中获取插件中自定义变量的税率。当然,我可以通过$order-> get_ ...请求很多数据,但是我找不到找到税率的方法(例如'21'-> 21%)。

有人想简单吗?

1 个答案:

答案 0 :(得分:2)

您将获得订单税项,这将为您提供WC_Order_Item_Tax对象数组,这些对象可以使用WC_Order_Item_Tax available methods来访问受保护的属性,例如:

// Get the the WC_Order Object from an order ID (optional)
$order = wc_get_order( $order_id );

foreach($order->get_items('tax') as $item_id => $item ) {
    $tax_rate_id    = $item->get_rate_id(); // Tax rate ID
    $tax_rate_code  = $item->get_rate_code(); // Tax code
    $tax_label      = $item->get_label(); // Tax label name
    $tax_name       = $item->get_name(); // Tax name
    $tax_total      = $item->get_tax_total(); // Tax Total
    $tax_ship_total = $item->get_shipping_tax_total(); // Tax shipping total
    $tax_compound   = $item->get_compound(); // Tax compound
    $tax_percent    = WC_Tax::get_rate_percent( $tax_rate_id ); // Tax percentage
    $tax_rate       = str_replace('%', '', $tax_percent); // Tax rate


    echo '<p>Rate id: '. $tax_rate_id . '<br>'; // Tax rate ID
    echo 'Rate code: '. $tax_rate_code . '<br>'; // Tax code
    echo 'Tax label: '. $tax_label . '<br>'; // Tax label name
    echo 'Tax name: '. $tax_name . '<br>'; // Tax name
    echo 'Tax Total: '. $tax_total . '<br>'; // Tax Total
    echo 'Tax shipping total: '. $tax_ship_total . '<br>'; // Tax shipping total
    echo 'Tax compound: '. $tax_compound . '<br>'; // Tax shipping total
    echo 'Tax percentage: '. $tax_percent . '<br>'; // Tax shipping total
    echo 'Tax rate: '. $tax_rate . '</p>'; // Tax percentage
}

经测试可正常工作

您也可以像在代码中那样使用WC_Tax available methods

请注意,您可以在订单中包含许多具有不同税率的'tax'项目。运输也可以有其自己的税率。同样的收费方式,就像您可以设置税种一样。还请记住,税率是根据客户所在位置确定的。


最后,您还可以使用一些WC_Abstract_Order methods来获取税额明细,例如:

// Get the the WC_Order Object from an order ID (optional)
$order = wc_get_order( $order_id );

$discount_tax         = $order->get_discount_tax();
$shipping_tax_total   = $order->get_shipping_tax();
$order_total_tax      = $order->get_total_tax();
$cart_total_tax       = $order->get_cart_tax();
$formatted_tax_totals = $order->get_tax_totals();