Hi @jpkarmatech
I understand you want the order grand total rounded to CHF 0.05, and you are seeing it look right on Checkout but revert after the order is placed. That happens because woocommerce_calculated_total only filters the cart’s running total for display, it does not persist into the created order. The reliable way is to add a small “rounding adjustment” as a fee during totals calculation, so the saved order total matches the rounded amount.
Please remove your current snippet and try this one instead:
/**
* Round the cart grand total to the nearest 0.05 CHF by adding an adjustment fee.
* Place in a small plugin or your child theme's functions.php.
*/
add_action( 'woocommerce_cart_calculate_fees', function( WC_Cart $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Get the current cart total as a raw number.
$total = (float) $cart->get_total( 'edit' ); // includes taxes and fees applied so far
// Round to nearest 0.05
$rounded = round( ($total + 0.000001) * 20 ) / 20;
// Delta to reach the rounded total
$delta = $rounded - $total;
// Only add an adjustment if needed
if ( abs( $delta ) >= 0.0005 ) {
// Third parameter false means do not tax this fee, since rounding applies to the final total
$cart->add_fee( __( 'Rounding adjustment', 'your-textdomain' ), $delta, false );
}
}, 20 );
This runs every time totals are recalculated, adds a tiny positive or negative fee to hit the nearest 0.05, and that value is stored on the order so it matches what customers saw at Checkout.
Please note that our scope of support here in the forum does not extend to writing or maintaining custom code. The snippet above is provided as a courtesy example to point you in the right direction. If you’d like further help refining or extending this, you may need to work with a Codeable developer or a WooExpert who can assist with customizations.
Give that a try and let us know how it goes!
Hi @lovingbro
Thank you very much for your help.
I replaced my old code with the one you provided, but unfortunately it doesn’t work. The amount is not rounded in the shopping cart or at checkout. The amount is also not rounded in the order that was placed.
I’ll keep trying.
Best regards
Hi @jpkarmatech
Thank you for testing that out and sharing the results. Since the snippet didn’t achieve the expected result on your setup, this may need a developer to look deeper into your theme, plugins, and tax setup to ensure the rounding works consistently. You could reach out to a developer via Codeable or a WooExpert for tailored assistance.
Also, if you’ve enjoyed using WooCommerce aside from this issue, we’d really appreciate it if you could take a moment to leave us a review here: https://ww.wp.xz.cn/support/plugin/woocommerce/reviews/#new-post. Your feedback helps us improve and support more users like you.
Hi @lovingbro
I have resolved the issue.
It is related to my transition to the new checkout template three months ago. After reverting to the classic checkout template, the rounding function with my aforementioned code is working again.
Best regards
Hi @jpkarmatech
I’m glad to hear you identified the cause and were able to resolve the rounding issue by switching back to the classic checkout template. It’s great that everything is working smoothly again.
Wishing you continued success with your store!