【问题标题】:Get the tax label per order item in Woocommerce 3在 Woocommerce 3 中获取每个订单项目的税标签
【发布时间】:2018-11-17 07:30:21
【问题描述】:

我正在尝试在 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

提前致谢

【问题讨论】:

    标签: php wordpress woocommerce orders tax


    【解决方案1】:

    以下代码从 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 中测试

    【讨论】:

    • 谢谢,这确实可以正常工作,并且 WooCommerce 在订单详细信息页面中使用了类似的方法,所以我想没有更简单的方法可以做到这一点。
    • 我做过类似的事情,也许对某些人有用:cart.php 模板带有一个额外的列,显示行项目的税额和税率:github.com/s-a-s-k-i-a/…
    • @Saskia 是的,但是钩子是更好的方法:1)当 WooCommerce 更新相关模板时,您不需要更新模板。 2)一些主题已经包含模板自定义,因此需要对您应该复制到子主题的主题模板进行必要的更改......我这里的答案代码只是展示了一种方法。
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 2012-05-22
    • 2019-08-29
    • 1970-01-01
    • 2018-12-23
    • 2018-07-11
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多