melvin
melvin

Reputation: 2621

Add a custom field value as a cart non taxable fee in Woocommerce

I have a custom field in checkout that adds a price to cart.

$woocommerce->cart->add_fee($price_info['label'], $fee, $taxable, $tax_class);

Everything works fine. But how do i make it non taxable ?

Upvotes: 1

Views: 802

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254483

To make a fee non taxable you need to set the 3rd argument to false, so your code will be:

WC()->cart->add_fee( $price_info['label'], $fee );

The default value for the 3rd argument is false, so no taxable. See its source code:

/**
 * Add additional fee to the cart.
 *
 * @uses WC_Cart_Fees::add_fee
 * @param string $name      Unique name for the fee. Multiple fees of the same name cannot be added.
 * @param float  $amount    Fee amount (do not enter negative amounts).
 * @param bool   $taxable   Is the fee taxable? (default: false).
 * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: '').
 */

public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) {
    $this->fees_api()->add_fee( array(
        'name'      => $name,
        'amount'    => (float) $amount,
        'taxable'   => $taxable,
        'tax_class' => $tax_class,
    ) );
}

Now if you use a negative fee (so a cart discount), it will be taxable even if you set it to false.

Upvotes: 3

Related Questions