How to programmatically set order item with the correct subtotal, for specific country, for product prices with tax included

Question

Our store uses prices with tax included and we are selling to Countries with different tax rates. We managed to get woocommerce to calculate everything perfectly with the filter:

add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );

But when we are trying to create an order programmatically with the code below:

$order = wc_create_order();
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );

$order->add_product( wc_get_product( '1234' ), 1 );

$order->calculate_totals();


$order->update_status( 'processing', 'Processing', TRUE );
$order->save();

Woocommerce always takes the same order item subtotal. When the totals are calculated the final price is wrong.
For example a product with price 69 should always get to the total of 69. If the tax rate is set to 19% the price is correct, but if the tax rate is set to 20% the total is 69,58 as it gets calculated from the wrong subtotal.

How to get Woocommerce to set the correct order item subtotal?

0
matija 2 years 2019-12-27T05:48:01-05:00 0 Answers 94 views 0

Leave an answer

Browse
Browse