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

社会主义新天地 提交于 2020-01-05 07:01:14

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/48901869/add-a-custom-field-value-as-a-cart-non-taxable-fee-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!