问题描述
是否有一种方法可以根据购物车中该产品的数量来创建适用于该产品的税种.
Is there a way to create a tax class that applies to a product based on the quantity of this product in the cart.
示例:如果同一产品的少于6件,则适用税款,否则不适用税款.
Example: If there is less then 6 items of the same product the taxes applies otherwise the taxes doesn't applies.
感谢您的帮助.
推荐答案
这是可能的.
首先在WooCommerce税收设置中创建一个税收类别,例如零税率",例如:
First create in WooCommerce Tax settings a tax class named for example "Zero Rate" like:
1)在税收选项"部分中添加零费率"并保存:
1) in Tax options sections add "Zero Rate" and save:
2)出现零利率"标签.在此标签部分下,将税项设置为零:
2) A tab "Zero rate" appear. Under this tab section set the tax to zero:
代码:
add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_taxes', 20, 1 );
function apply_conditionally_taxes( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach( $cart->get_cart() as $cart_item ){
if( $cart_item['quantity'] >= 6 ){
$cart_item['data']->set_tax_class('zero-rate');
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于Woocommerce中基于购物车项目数量应用的税收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!