在 Woocommerce 中添加自定义字段值作为购物车非应税费用
Add a custom field value as a cart non taxable fee in Woocommerce
我在结账时有一个自定义字段,用于将价格添加到购物车。
$woocommerce->cart->add_fee($price_info['label'], $fee, $taxable, $tax_class);
一切正常。但是我如何让它免税?
要使费用免税,您需要将第三个参数设置为 false,因此您的代码将是:
WC()->cart->add_fee( $price_info['label'], $fee );
第三个参数的默认值为 false
,因此无需征税。查看其源代码:
/**
* 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
.
我在结账时有一个自定义字段,用于将价格添加到购物车。
$woocommerce->cart->add_fee($price_info['label'], $fee, $taxable, $tax_class);
一切正常。但是我如何让它免税?
要使费用免税,您需要将第三个参数设置为 false,因此您的代码将是:
WC()->cart->add_fee( $price_info['label'], $fee );
第三个参数的默认值为 false
,因此无需征税。查看其源代码:
/**
* 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
.