在 WooCommerce 3.0+ 中一次限制购买 1 个产品类别
Limit purchase to 1 product category at the time in WooCommerce 3.0+
我已将 WooCommerce 更新到版本 3.0+,此自定义功能不再像以前那样工作了。即使我的购物车是空的,我也会收到错误消息,就好像我的购物车中已经有其他类别的东西一样。
这是我使用的代码:
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); //get the current items
foreach ($terms as $term) {
$cat_ids[] = $term->term_id; //get all the item categories in the cart
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id; //get all the categories of the product
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
如何让它适用于 WooCommerce 3.0+ 版,知道吗?
谢谢
您应该尝试执行相同操作的不同代码:
add_filter( 'woocommerce_add_to_cart_validation', 'checking_conditionaly_product_categories', 10, 3 );
function checking_conditionaly_product_categories( $passed, $product_id, $quantity) {
// Getting the product categories slugs in an array for the current product
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat)
$product_cats[] = $obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_values ){
// When the product category of the current product match with a cart item
if( has_term( $product_cats, 'product_cat', $cart_item_values['product_id'] ))
{
// Displaying a message
wc_add_notice( 'Only one product from each category is allowed in cart', 'error' );
$passed = false; // Set to false
break; // stop the loop
}
}
return $passed;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码已经过测试,适用于 WooCommerce 版本 2.6+ 和 3.0+
相似答案:
我已将 WooCommerce 更新到版本 3.0+,此自定义功能不再像以前那样工作了。即使我的购物车是空的,我也会收到错误消息,就好像我的购物车中已经有其他类别的东西一样。
这是我使用的代码:
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); //get the current items
foreach ($terms as $term) {
$cat_ids[] = $term->term_id; //get all the item categories in the cart
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id; //get all the categories of the product
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
如何让它适用于 WooCommerce 3.0+ 版,知道吗?
谢谢
您应该尝试执行相同操作的不同代码:
add_filter( 'woocommerce_add_to_cart_validation', 'checking_conditionaly_product_categories', 10, 3 );
function checking_conditionaly_product_categories( $passed, $product_id, $quantity) {
// Getting the product categories slugs in an array for the current product
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat)
$product_cats[] = $obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_values ){
// When the product category of the current product match with a cart item
if( has_term( $product_cats, 'product_cat', $cart_item_values['product_id'] ))
{
// Displaying a message
wc_add_notice( 'Only one product from each category is allowed in cart', 'error' );
$passed = false; // Set to false
break; // stop the loop
}
}
return $passed;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码已经过测试,适用于 WooCommerce 版本 2.6+ 和 3.0+
相似答案: