Is there any way to remove a fee from an Woocommerce order before saving it to the database?
I have tried the following hooks, to no success
- woocommerce_before_save_order_items
- woocommerce_calculate_totals
- wp_insert_post_data
I also tried to edit the fee total as below, but the fee still gets saved to the database
add_action( 'woocommerce_review_order_before_payment', 'cst_my_hook_1', 10, 3);
function cst_my_hook_1() {
WC()->cart->set_fee_total(0);
}
I am sharing a screenshot to make my requirements more clear. Woocommerce cart class (class-wc-cart.php) contains a public function to add fees, so I think there should be ways to remove it too.
I used the hook "woocommerce_cart_calculate_fees" to add the fee shown in the screenshot. Now I want to remove it before saving to DB.
I am using WordPress 5.7.1, Woocommerce 5.2.1
2
Answers
To disable fees from order once checking out, use this simple following hooked function, that will clean all fees from orders and email notifications:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now if you want to remove the fees, but keep original total order amount, use the following:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
If you want to remove a fee from an order before saving it in the database you can use the
woocommerce_create_order
hook that fires before the order is created within thecreate_order
method of theWC_Checkout
class.Based on this answer:
you will be able to remove the fee from the cart.
REMOVE A FEE FROM CART AND RECALCULATE TOTALS
REMOVES A FEE FROM CART WITHOUT RECALCULATING THE TOTALS
The code has been tested and works. Add it to your active theme’s functions.php.