WooCommerce:仅从税率降低的商品中获取订单总额 [英] WooCommerce: Get order totals only from items with reduced tax rate

查看:70
本文介绍了WooCommerce:仅从税率降低的商品中获取订单总额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想导出税率降低的项目的总计.

I want to export only the totals from items with a reduced tax rate.

例如,我有 2 件商品的税率降低了,一件商品的税率正常:

For example, I have 2 items with a reduced tax rate and one with the normal tax rate:

  • 项目 A:100 欧元 - 减税 5,00 欧元 - 总计 105,00 欧元
  • 项目 B:50 欧元 - 减税 2.50 欧元 - 总计 52.50 欧元
  • 项目 C:100 欧元 - 19,00 欧元正常税 - 总计 119,00 欧元

我现在想要项目 A 和 B 的总计.在本例中,合计值为 157,50 欧元.

I now want the totals from Item A and B. In this case a combined value of 157,50€.

我发现了一个 代码片段以获取类别的总税额:

I found a code snippet to get the total tax amounts with class:

// Get the WC_Order instance Object from the Order ID (if needed)
$order = wc_get_order($order_id);

// Output the tax rows in a table
echo '<table>';
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
    $tax_rate_id  = $tax->rate_id;
    $tax_label    = $tax->label;
    $tax_amount   = $tax->amount;
    $tax_f_amount = $tax->formatted_amount;
    $compound     = $tax->is_compound;
    echo '<tr><td>' . $tax_label  . ': </td><td>' . $tax_f_amount . '</td></tr>';
}
echo '</table>';

但该代码为我提供了所有税种.有没有办法只得到一个?

But that code gives me all tax classes. Is there any way to get only one?

这是我现在正在做的事情(摘录):

Here's what I'm doing right now (excerpt):

$custom_order_total_tax         = $custom_order_data['total_tax'];
$custom_order_total             = $custom_order_data['total'];      
$custom_order_subtotal          = $order->get_subtotal();

我不能在 $order 变量中使用任何东西.

There is nothing I could use in the $order variable.

我想我需要检查订单的所有项目.但我如何按税率选择这些项目?

I guess I need to go through all items of the order. But how could I select these items by tax rate?

我发现了一些东西 这里:

// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product();
   $name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $type = $item->get_type();
}

但税率不是其中的一部分.所以我想我需要第二个数组?

But the tax rate is not part of it. So I guess I need a second array?

推荐答案

要获取订单商品的税级,请看这里:

To get the tax class of the order items look here:

然后您可以创建一个自定义函数,该函数根据指定的税种对订单商品的总数求和.

Then you can create a custom function that sums the totals of the order items based on a specified tax class.

该函数有两个参数:

  • $order_id:要从中获取总数的订单的 ID
  • $tax_class:用作过滤器的税种(默认设置为reduced-rate")

所以:

// gets the total of the order items by tax class
function get_total_order_items_by_tax_class( $order_id, $tax_class = 'reduced-rate' ) {
    $order = wc_get_order( $order_id );
    // initializes the total of the order items
    $total = 0;
    foreach( $order->get_items() as $item_id => $order_item ) {
        // if the product tax class is equal to "$tax_class"
        if ( $tax_class == $order_item['tax_class'] ) {
            // sum the total
            $total += $order_item['total'];
        }
    }
    return $total;
}

用法

如果您想根据<获得订单id60的订单商品的总和code>reduced-rate tax class 你可以这样做:

If you want to get the sum of the totals of the order items with the order id 60 according to the reduced-rate tax class you can do it as follows:

$total = get_total_order_items_by_tax_class( 60 );

如果您想根据standard税种过滤产品,您可以使用:

If you want to filter products based on the standard tax class you can use:

$total = get_total_order_items_by_tax_class( 60, '' );

因为 standard 税类 slug 是空的.

Because the standard tax class slug is empty.

代码已经过测试并且可以正常工作.

这篇关于WooCommerce:仅从税率降低的商品中获取订单总额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆