0

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). right amounts ( 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.

wrong amounts

(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

1 Answer 1

0

Well, I succeeded in filtering the output of the totals :

add_filter( 'woocommerce_order_get_tax_totals', 'filter_function_name_1942', 10, 2 );
function filter_function_name_1942( $tax_totals, $order ){
    
    foreach( $tax_totals as $single_tax_key => $single_tax_value ){
        if( $single_tax_value->rate_id == 1){
            $tax_totals[$single_tax_key]->amount = 1000;
            $tax_totals[$single_tax_key]->formatted_amount = wc_price('1000');
        }
    }
    
    return $tax_totals;
}

It's just an example with hardcoded values, but it works.

The problem I see now is that with this way the value displayed is right but the value recorded in the database is still wrong (I suppose). I'm right ? Is it necessary to save the good value in the database too ? if the value is used by an other plugin for example. If yes, an idea about how to do it ? I can't find a method to save the custom value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.