将零税 class 设置为 Woocommerce 中定义的产品类别中的特定项目计数
Set zero tax class to specific item count from defined product categories in Woocommerce
在 WooCommerce 中,如果购物车包含来自 2 个特定类别的 6 件或更多商品,那么我想只对这些商品(不是整个购物车,所以不要为其他产品更改它)。
我使用这段代码来计算购物车中来自 2 个类别的商品数量,但我找不到如何完成它以便用我的 "tax-zero" 税 class.
add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_taxes', 20, 1 );
function apply_conditionally_taxes( $cart ){
$item_count = $cart->get_cart_contents_count();
$kingcat_count = 0;
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( 'patisseries', 'product_cat', $cart_item['product_id'] ) or has_term( 'viennoiseries-et-gaufres', 'product_cat', $cart_item['product_id'] ) ) {
$kingcat_count += $cart_item['quantity'];
//echo $kingcat_count;
}
}
}
代码包含:(作为注释添加到代码中的说明)
- 只计算属于某个类别的项目
- 仅对这些项目设置特定税费class
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
/* SETTINGS */
// Set categories
$categories = array ( 'patisseries', 'viennoiseries-et-gaufres' );
// Contains 6 or more items
$contains_min = 6;
/* END SETTINGS */
// Counter
$counter = 0;
// Loop trough
foreach ( $cart->get_cart() as $cart_item ) {
// Break loop
if ( $counter >= $contains_min ) {
break;
}
// Has term
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// Add to counter
$counter += $cart_item['quantity'];
}
}
// Check
if ( $counter >= $contains_min ) {
// Loop trough
foreach( $cart->get_cart() as $cart_item ) {
// Has term
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// We set "Zero rate" tax class
$cart_item['data']->set_tax_class( 'Zero rate' );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
在 WooCommerce 中,如果购物车包含来自 2 个特定类别的 6 件或更多商品,那么我想只对这些商品(不是整个购物车,所以不要为其他产品更改它)。
我使用这段代码来计算购物车中来自 2 个类别的商品数量,但我找不到如何完成它以便用我的 "tax-zero" 税 class.
add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_taxes', 20, 1 );
function apply_conditionally_taxes( $cart ){
$item_count = $cart->get_cart_contents_count();
$kingcat_count = 0;
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( 'patisseries', 'product_cat', $cart_item['product_id'] ) or has_term( 'viennoiseries-et-gaufres', 'product_cat', $cart_item['product_id'] ) ) {
$kingcat_count += $cart_item['quantity'];
//echo $kingcat_count;
}
}
}
代码包含:(作为注释添加到代码中的说明)
- 只计算属于某个类别的项目
- 仅对这些项目设置特定税费class
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
/* SETTINGS */
// Set categories
$categories = array ( 'patisseries', 'viennoiseries-et-gaufres' );
// Contains 6 or more items
$contains_min = 6;
/* END SETTINGS */
// Counter
$counter = 0;
// Loop trough
foreach ( $cart->get_cart() as $cart_item ) {
// Break loop
if ( $counter >= $contains_min ) {
break;
}
// Has term
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// Add to counter
$counter += $cart_item['quantity'];
}
}
// Check
if ( $counter >= $contains_min ) {
// Loop trough
foreach( $cart->get_cart() as $cart_item ) {
// Has term
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// We set "Zero rate" tax class
$cart_item['data']->set_tax_class( 'Zero rate' );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );