I'm developing a plugin based on WooCommerce to allow ordering gift boxes. Each box is a WooCommerce product composed by multiple "alternative products" (that are made from a custom post type). For some reasons (a box can have multiple taxe rates, etc) I need to override taxes amounts when ordering. I succeeded this step by basicaly doing this following :
add_action( 'woocommerce_calculate_totals', 'custom_calc_total', 999, 1 );
function custom_calc_total( $cart ) {
//here I calculate amounts of differents taxes depending of what my gift boxes contain)
//and here I override tax amount of the cart with my values
$cart->set_cart_contents_taxes(
array(
1 => $total_tax20, //french tax 20%
5 => $total_tax5, //french tax 5.5%
//=> ids 1 and 5 depend of my website
)
);
}
The customer can finalise its order and if the merchant goes backend to the order panel, we can see the right amounts for taxes (I mean the custom calculated ones). ( A : my boxes and the custom taxes amounts stored in products metas in case of ; B : the right amounts when the order is not updates yet)
But if the merchant update the order, to change the status from "pending" to "complete" for example, I think WooCommerce recalculates and updates some of the values (totals, taxes, etc). So as my woocommerce product "Gift box" is a kind of "fake product" whose values (price, weight, taxes, etc) are overwritten during the buying process depending of what the box contains, when WooCommerce recalculates the results are wrong.
(after the order update, the amount of tax is wrong. Here there is only shipping tax because the Box is a Woocoomerce product with a flate rate)
So, how (which hook and which function) to allow my plugin overriding taxes amounts when an order is updated as I do it during the buying process ?
I have found the hook woocommerce_order_after_calculate_totals , and others like here https://stackoverflow.com/questions/56701407/woocommerce-add-order-admin-hook-for-recalculate-button
and I have found some useful functions as $order->get_total_tax(), $order->get_cart_tax(), $order->get_tax_totals(), $order->get_taxes(), $order->set_cart_tax()
so I tried something like this :
add_action( 'woocommerce_order_after_calculate_totals', 'woocommerce_order_after_calculate_totals_test', 999,2);
function woocommerce_order_after_calculate_totals_test ($and_taxes, $order){
$order->set_cart_tax( 1000 );//change the total value of taxes and not
$order->save();
}
but I miss something and I cannot manage doing what I want.
I have also seen stuff like this if it can help, but I did not try yet. Maybe it's a track to explore ? :
apply_filters( 'woocommerce_order_get_tax_totals', $tax_totals, $this );
and
$order->update_meta_data( '_' . $key, $value ); ?
So if you have an idea it would be awesome.
Thank you