The Tankgirl
The Tankgirl

Reputation: 93

Woocommerce pdf invoice: filter /sort products by tax rate_id and calculate item prices (sum per tax rate id)

I'm making a custom calculation on the woocommerce invoice. As a plugin I'm using woocommerce-pdf-invoices, and the calculations will not be saved in the database.

I'm stuck at this point: for the item prices, I have to use foreach $order->get_items()

To filter all the products on tax rate id, I have to use a foreach $order->get_items('tax')

How can I: - load all the items (tax, and prices) - filter / sort them by tax_rate_id

This so I can calculate the item_prices per tax rate id (so I have to get the item prices also)

When I use foreach for the tax, and then a foreach for the prices, the items are doubled on the invoice, and the totals for each tax_rate_id are the same.

Here is an example of my (failed) piece of code

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

By the way, if there is an easier way to get the subtotal per tax rate id, please tell me. I normally work with magento, and still learning each day.

so what I want is: Tax rate 1 = (sum of product prices with tax rate 1) Tax rate 2 = (sum of product prices with tax rate 2)

Upvotes: 2

Views: 443

Answers (1)

The Tankgirl
The Tankgirl

Reputation: 93

I got the answer, unfortunately not completely, but it works. I still would like to get the tax rate code.

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 ;


}


}
}

Upvotes: 0

Related Questions