Woocommerce pdf发票:按tax rate_id筛选/排序产品并计算商品价格(每个税率id的总和)

时间:2019-02-15 08:48:41

标签: php woocommerce

我正在对woocommerce发票进行自定义计算。作为一个插件,我正在使用woocommerce-pdf-invoices,并且计算结果不会保存在数据库中。

我被困在这一点上: 对于项目价格,我必须使用foreach $ order-> get_items()

要按税率ID过滤所有产品,我必须使用foreach $ order-> get_items('tax')

我如何:  -加载所有项目(含税和价格)  -按tax_rate_id进行过滤/排序

这样,我就可以计算每个税率ID的item_prices(因此,我也必须获取项目价格)

当我使用foreach作为税收,然后使用foreach作为价格时,项目在发票上加倍,并且每个tax_rate_id的总数相同。

这是我(失败的)一段代码的示例

if ( sizeof( $order->get_items() ) > 0 ) {
    foreach ( $order->get_items() as $item_key => $item_values ):
        foreach ( $order->get_items( 'tax' ) as $item_id => $item_tax ) :


            $item_id = $item_values->get_id();


            $item_name = $item_values->get_name(); // Name of the product
            $item_type = $item_values->get_type(); // Type of the order item ("line_item")

            $product_id = $item_values->get_product_id(); // the Product id
            $product    = $item_values->get_product(); // the WC_Product object


            $item_data = $item_values->get_data();

            $product_name      = $item_data['name'];
            $product_id        = $item_data['product_id'];
            $tax_class         = $item_data['tax_class'];
            $line_subtotal     = $item_data['subtotal'];
            $line_subtotal_tax = $item_data['subtotal_tax'];
            $line_total        = $item_data['total'];
            $line_total_tax    = $item_data['total_tax'];
            $item_tax_rate_id  = $item_tax['rate_id'];
            $item_tax_label    = $item_tax['label'];


            $product_type   = $product->get_type();
            $product_sku    = $product->get_sku();
            $product_price  = $product->get_price();
            $stock_quantity = $product->get_stock_quantity();

            if ( $item_tax_rate_id == 3 ) {


                $hoog += $product_price;

            }
            if ( $item_tax_rate_id == 1 ) {


                $laag += $product_price;

            }

        endforeach;
    endforeach;
}

顺便说一句,如果有一种更简单的方法来获取每个税率ID的小计,请告诉我。我通常与magento合作,并且每天仍在学习。

所以我想要的是: 税率1 =(含税率1的产品价格之和) 税率2 =(含税率2的产品价格之和)

1 个答案:

答案 0 :(得分:0)

不幸的是,我没有得到答案,但是可以。我仍然想获取税率代码。

foreach( $order->get_items() as $item ){
$tax_rates = WC_Tax::get_rates( $item->get_tax_class() );
$item_price = $item->get_total() + $item->get_total_tax();
if (!empty($tax_rates)) {
    $tax_rate = reset($tax_rates);
    $tax_rate =$tax_rate['rate'];
if (($tax_rate == 6) || ($tax_rate == 9)){

$low += $item_price ;



} 

if ($tax_rate == 21 ){

$high += $item_price ;


}


}
}
相关问题