Home > Software engineering >  Programatically added fee in WooCommerce not being taxed
Programatically added fee in WooCommerce not being taxed

Time:11-13

I have added the following to create additional cart fees for surcharges based on weight, but tax isn’t being applied to the additional fees – what am I missing?

The site in question is http://oldhousestore.co.uk/

CODE CREATED

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Convert cart weight in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 0.00697; // Starting Fee below 1000g

    // Above
    if( $cart_weight > 1000 ){
        for( $i = 1000; $i < $cart_weight; $i  = 1000 ){
            $fee  =0.00697;
        }
    }
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Energy Surcharge' ), $fee, false );

    // Convert cart weight in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 0.00165; // Starting Fee below 1000g

    // Above
    if( $cart_weight > 1000 ){
        for( $i = 1000; $i < $cart_weight; $i  = 1000 ){
            $fee  =0.00165;
        }
    }
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Environmental Surcharge' ), $fee, false );
}

EXPECTED: Tax total to include Surcharges, Cart Items and Shipping

OBSERVED: Tax total only includes Cart items and shipping

CodePudding user response:

The third parameter for add_fee sets wether or not the fee is taxable:

WC_Cart::add_fee( $name, $amount, $taxable, $tax_class );

You set this to false on this line: $cart->add_fee( __( 'Energy Surcharge' ), $fee, false );

You should at least set this to true, and perhaps provide the name for the tax class.

  • Related