Change order total after checkout in Woocommerce

10,843

This can be done in woocommerce_checkout_create_order action hook, where you will have to use CRUD getters and setters methods for WC_Abstract_Order and WC_Order classes...

As cart object and cart session has not been destroyed yet, you can still also use WC()->cart object and WC_Cart methods, to get data…

This hook is triggered just before the order data is saved in database with $order->save();. You can see that in the source code HERE.

Below a fake working example:

add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
    // Get order total
    $total = $order->get_total();

    ## -- Make your checking and calculations -- ##
    $new_total = $total * 1.12; // <== Fake calculation

    // Set the new calculated total
    $order->set_total( $new_total );
}

Code goes in function.php file of your active child theme (or theme).

Tested and works.

Some explanations here: Add extra meta for orders in Woocommerce

Share:
10,843
Shile
Author by

Shile

Hi, I am a freelance full stack web developer and i have been a Top Rated freelancer on Upwork ( https://www.upwork.com/fl/nemanjaivanis ) since 2016. I am easy to communicate and i tend to develop long term relationships with my clients as you can see on my reviews. I try to keep my work transparent so i always upload my work on my personal server so the clients can see all the phases of my development process. I am a local Hackathon award winner and my preferred development stack is Laravel+Vuejs+Bootstrap. Frontend Skills: -JavaScript,Vuejs,jQuery,jQuery UI,Bootstrap,CSS3,HTML5,Webpack,Bulma,Chart.js,DataTables,SASS Backend Skills: - PHP,Laravel,MySQL,MySQLite CMS Skills: - Wordpress,Joomla API integrations: - Google API,Twilio,Stripe,Paypal,Trello,Slack Other skills: - API development - Importing and manipulating excel and csv files - Web scraping I am available every day from 12:00 to 00:00 CEST(UTC +2) so feel free to contact me. Looking forward on working on your next project. Cheers, Nemanja

Updated on August 25, 2022

Comments

  • Shile
    Shile over 1 year

    I cant seem to find which hook to use to change the total (or any variable of the cart) after user clicks checkout. So for example, user Submits the Checkout form and then I need to do some checks and change the total accordingly.

    How should I do that, which hook do I use?

  • Shile
    Shile about 6 years
    Thanks for this, i spent a lot of time searching and couldnt find this particular hook.
  • Shile
    Shile about 6 years
    Sorry for bothering you again, but how do i set the subtotal and product price in the same process? There is no setter like set_subtotal() in the WC_Abstract_Order.
  • LoicTheAztec
    LoicTheAztec about 6 years
    @Shile for order subtotal that is much more complicated as it's a calculated amount based on line items… so you should better ask a new question, with the minimal code you have tried and some related details about what you want to do and what you are checking… for line items you should look at WC_Checkout create_order_line_items() method source code