[英]Get tax rates for product on manual order creation in WooCommerce admin
[英]How to get order tax details and rates in WooCommerce?
我正在嘗試從訂單中獲取插件中自定義變量的稅率百分比。 當然,我可以通過$order-> get_...
請求大量數據,但我找不到獲取稅率的方法(例如 '21' -> 21%)。
任何人都有想法讓這個簡單嗎?
您將必須獲取訂單稅項,這將為您提供一組WC_Order_Item_Tax
對象,使用WC_Order_Item_Tax
可用方法可以訪問這些受保護的屬性,例如:
// 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
可用方法
請注意,您可以在一個訂單中包含許多具有不同稅率的“稅”項目。運輸也可以有自己的稅率。費用也是如此,因為您可以設置稅 class。還請記住,稅率基於客戶所在地.
最后,您還可以使用一些WC_Abstract_Order
方法來獲取稅額詳細信息,例如:
// 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.