在 Woocommerce 3 中获取每个订单商品的税号

Jan*_*ser 2 php wordpress orders woocommerce tax

我正在尝试在 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);
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

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" } }
Run Code Online (Sandbox Code Playgroud)

因此,由于这没有给我每个行项目的税费标签,我尝试了以下操作:

$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());
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer" string(14) "Mehrwertsteuer"
Run Code Online (Sandbox Code Playgroud)

但它应该是“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());
}
Run Code Online (Sandbox Code Playgroud)

这输出:

string(10) "19 % MwSt."
Run Code Online (Sandbox Code Playgroud)

所以是的,这就是我需要的值,但为什么它只输出一次这个值,因为我的订单中有七个项目?

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

WooCommerce 版本是 3.4.2

提前致谢

Loi*_*tec 6

以下代码从 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>';
Run Code Online (Sandbox Code Playgroud)

在 Woocommerce 3.4.2 版中测试