Cray
Cray

Reputation: 5483

WooCommerce: Get order export with tax lines

I want to export all my orders with the tax lines separated by tax rate.

For example: An order could have the tax rates 7% and 19%. If the order has products with both tax rates, I only get one combined tax amount.

Let's say the order has two products for 100€ each. There would be the following taxes:

I'm using the following code (excerpt) to export order data:

$order_data = $order->get_data(); // The Order data 
$order_date_created = $order_data['date_created']->date('d.m.Y'); // Example for oder creation date

After getting the data, I need to store them in an array:

$custom_data = array(
    'custom_order_date_created'     => $custom_order_date_created,
)

What I need is a fixed column (like the $order_date_created above) for every tax rate.

Like:

If there is no amount for a tax rate the column could be empty.

I know that there is an order property called tax_lines. But it's an array and I don't know how to use it. The property tax_lines has some own tax lines properties.

Does anyone has a clue how to get these values from an order?

EDIT: I found a snippet in another question:

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

It looks right, but I'm not sure how to use it with my code example and store it in my array.

EDIT 2: After some more digging I found the WC_Order_Item_Tax object which has the order tax items in it. So I tried to use that data but it is an huge object and I'm not sure how to select the data and show it in the right column.

It looks like this (there is a lot more). And I need to select the tax by the rate_id for example:

[4543] => WC_Order_Item_Tax Object
        (
            [extra_data:protected] => Array
                (
                    [rate_code] => 
                    [rate_id] => 0
                    [label] => 
                    [compound] => 
                    [tax_total] => 0
                    [shipping_tax_total] => 0
                    [rate_percent] => 
                )

            [data:protected] => Array
                (
                    [order_id] => 11244
                    [name] => 
                    [rate_code] => DE-MWST.-1
                    [rate_id] => 1
                    [label] => MwSt.
                    [compound] => 1
                    [tax_total] => 7.54
                    [shipping_tax_total] => 0
                    [rate_percent] => 16
                )

            [cache_group:protected] => order-items
            [meta_type:protected] => order_item
            [object_type:protected] => order_item
            [id:protected] => 4543
            [changes:protected] => Array
                (
                )

            [object_read:protected] => 1
            [default_data:protected] => Array
                (
                    [order_id] => 0
                    [name] => 
                    [rate_code] => 
                    [rate_id] => 0
                    [label] => 
                    [compound] => 
                    [tax_total] => 0
                    [shipping_tax_total] => 0
                    [rate_percent] => 
                )

Upvotes: 2

Views: 1180

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253901

You can use it this way may be after this code line

$custom_data = array(
    'custom_order_date_created'     => $custom_order_date_created,
)

Then the foreach loop with tax rate items and using WC_Tax method get_rate_percent() to get the tax rate percentage as follows:

// Loop through order tax items
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {

    // Store it in the array
    $custom_data['taxes'][] = array(
        'tax_rate'   =>  WC_Tax::get_rate_percent($tax->rate_id), // <=== the tax rate percentage
        'tax_amount' => $tax->formatted_amount
    );
}

Related:

Upvotes: 2

Related Questions