Reputation: 43
I have a WooCommerce site where products may have a non-taxable deposit fee, may have a delivery fee ($7.50), and may have a discount. Without applying the negative fee (discount) the tax is calculated correctly. Once I add the negative fee, the tax includes the non-taxable deposit fee in its calculation. I read somewhere that negative fees are not recommended. I also found this post but don't know if this applies here. Is there another way to accomplish this in the cart and also show in the orders, emails, etc.? FYI the tax rate is 15%. Here's the code I'm using:
function woocommerce_custom_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$item_data = $cart_item['data'];
$deposit = $item_data->get_attribute('deposit');
$delivery = $cart_item['delivery'];
if ( $deposit ) {
$total_deposit += $cart_item['quantity'] * $deposit;
}
if ( $delivery == 'deliver' ) {
$total_delivery += $cart_item['quantity'] * 7.5;
}
}
if ( $total_deposit > 0 ) {
// non-taxable
$cart->add_fee( 'Deposit', $total_deposit, FALSE );
}
if ( $total_delivery > 0 ) {
// taxable
$cart->add_fee( 'Delivery', $total_delivery, TRUE );
}
// test $10 discount
$cart->add_fee( 'Test discount', -10.00);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );
correct tax amount without negative fee
incorrect tax amount with negative fee
UPDATE: I found this post Apply a discount on the cart content total excluding taxes in WooCommerce which says that using a negative fee will cause the taxes to always get applied. Is there an alternative method to apply discounts in the cart other than to use negative fees or coupons?
Upvotes: 2
Views: 2120
Reputation: 11841
function woocommerce_custom_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// non-taxable
$cart->add_fee( 'Deposit', 6, FALSE );
// taxable
$cart->add_fee( 'Delivery', 7, TRUE );
// test $10 discount
//$cart->add_fee( 'Test discount', -10.00 , FALSE);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );
add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
function discounted_calculated_total( $total, $cart ){
$total = $total - 10;
return $total;
}
Tried this way?
Upvotes: 0