gpt4 book ai didi

php - 在 Woocommerce 3 中获取每个订单商品的税标

转载 作者:行者123 更新时间:2023-12-05 01:42:46 24 4
gpt4 key购买 nike

我正在尝试在 WooCommerce 中获取每个订单商品的税务标签。

例如:2x 产品 1 - 19 % MwSt. (税)4x 产品 2 - 19 % MwSt. (税)1x 产品 2 - 19 % MwSt. (税)

因此,我在 woocommerce 设置中添加了“19 %(税)”作为标准值和“7 %(税)”作为减税。我还设置了我想在 WooCommerce 中“免税”显示和输入价格。

现在我用这两个项目(数字和可下载的)下了一个订单,然后我尝试获取每个订单行项目的税值。

$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);
}

这是输出:

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" } }

因为这没有给我每个订单项的税费标签,所以我尝试了以下操作:

$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());
}

这是输出:

string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer"

但它应该是“19 % MwSt”。

所以我尝试了这个:

$wc_order = wc_get_order(143);
foreach ($wc_order->get_items(array('tax')) as $item_id => $line_item) {
var_dump($line_item->get_label());
}

这个输出:

string(10) "19 % MwSt."

是的,这就是我需要的值,但为什么它只输出一次这个值,因为我的订单中有七件商品?

是否有机会获得每个订单行项目的税务标签,或者在 WooCommerce 中不可能?我的目的是创建一个发票程序,但我必须考虑每行是否有不同的税率。

WooCommerce 版本是 3.4.2

提前致谢

最佳答案

以下代码自 Woocommerce 版本 3 起有效。

它将允许您获得税务标签,因为您可以在一个订单中添加多个,具体取决于您的税务设置、产品设置和运输设置。

// 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>';

在 Woocommerce 版本 3.4.2 中测试

关于php - 在 Woocommerce 3 中获取每个订单商品的税标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com