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
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
To disable fees from order once checking out, use this simple following hooked function, that will clean all fees from orders and email notifications:
add_action( 'woocommerce_checkout_order_created', 'order_created_disable_fees' );
function order_created_disable_fees( $order ) {
$targeted_item_name = __( "Total Tax Payment", "woocommerce" );
foreach( $order->get_items( 'fee' ) as $item_id => $item ) {
if( $targeted_item_name === $item['name'] ) {
$order->remove_item($item_id);
}
}
$order->calculate_totals();
}
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:
add_action( 'woocommerce_checkout_order_created', 'order_created_disable_fees' );
function order_created_disable_fees( $order ) {
$targeted_item_name = __( "Total Tax Payment", "woocommerce" );
foreach( $order->get_items( 'fee' ) as $item_id => $item ) {
if( $targeted_item_name === $item['name'] ) {
$order->remove_item($item_id);
}
}
$order->save();
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.