I try to add fees based on some calculations I do on Woocommerce cart, but I want to exclude it from VAT. This is my code:
function woo_add_cart_fee( $cart ) {
global $woocommerce; $bookable_total = 0;
foreach(WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
//doing my stuff to calculate $fee variable
WC()->cart->add_fee( 'Fees: ', $fee, false, '' );
//WC()->cart->add_fee( 'Fees: ', $fee, true, '' );
//WC()->cart->add_fee( 'Fees: ', $fee, false, 'zero rate' );
//WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
I have tried all the commented versions and each of them includes a VAT on Fees too.
Any idea how I can achieve it?
(Update): TAX OPTIONS with add_fee() method
For example if you want to use 'zero rate' tax class, but you haven't defined the correct 'zero rate' tax class for the customer country, this will not work if you try to use it with:
WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
… depending on your global tax settings.Here is a screenshot of REAL checkout totals, for 3 items in cart (using the code below):
Original answer (updated code):
We can defined now
$bookable_total
variable value outside the function.This is a working example with your code:
This code is tested and it works. It goes on function.php file of your active child theme or theme.
If the
$bookable_total
variable is not defined outside, the value will be0
.Reference:
Class WC_Cart -
add_fee( $name, $amount, $taxable = false, $tax_class = '' )
method