'Woocommerce - manually add tax to cart

In woocommerce store I add manually product to cart by WC()->cart->add_to_cart() function. I need add tax to this cart/product (in product menu I have selected tax class). When I complete order, I don't have tax column in order menu in ACP. I tried with WC()->cart->add_fee() and set_tax_class() but it don't work.

Maybe I'm doing something wrong, I will be very grateful for any help, thanks.



Solution 1:[1]

I've always used the following and it works fine. You need to hook into the woocommerce_cart_calculate_fees hook.

add_action( 'woocommerce_cart_calculate_fees','rs_packaging_fee' );



function rs_packaging_fee() {      
    global $woocommerce;
    //Set the Fee
    $materials_surcharge = '5.00';//if negative it will subtract from total.
    //add the fee to the cart
    $woocommerce->cart->add_fee( 'Metals Surcharge', $materials_surcharge, true, 'standard' );

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ){           
        return;             
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2355051