[英]Restricting cart items to be from the same product category in WooCommerce
I am using the code below to remove other WooCommerce product category items from the cart when there is an item with a special product category 'cat_x' added in cart and display some different custom notices.当购物车中添加了具有特殊产品类别“cat_x”的项目并显示一些不同的自定义通知时,我正在使用下面的代码从购物车中删除其他 WooCommerce 产品类别项目。 The code came from this thread and just works well:
代码来自这个线程并且运行良好:
add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );
function checking_cart_items() {
$special = false;
$catx = 'cat_x';
$number_of_items = sizeof( WC()->cart->get_cart() );
if ( $number_of_items > 0 ) {
// Loop through all cart products
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$item = $values['data'];
$item_id = $item->id;
// detecting if 'cat_x' item is in cart
if ( has_term( $catx, 'product_cat', $item_id ) ) {
if (!$special)
$special = true;
}
}
// Re-loop through all cart products
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$item = $values['data'];
$item_id = $item->id;
if ( $special ) // there is a 'cat_x' item in cart
{
if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart
if ( empty( $notice ) )
$notice = '1';
}
if ( $number_of_items >= 2 ) { // 'cat_x' item + other categories items in cart
// removing other categories items from cart
if ( !has_term( $catx, 'product_cat', $item_id ) ) {
WC()->cart->remove_cart_item( $cart_item_key ); // removing item from cart
if ( empty( $notice ) || $notice == '1' )
$notice = '2';
}
}
} else { // Only other categories items
if ( empty( $notice ) )
$notice = '3';
}
}
// Firing notices
if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)
wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla one category X item in the cart</p>' ), 'success' );
} elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones
wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>' ), 'error' );
} elseif ( $notice == '3' ) { // message for other categories items (if needed)
wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>' ), 'success' );
}
}
}
Has the conditional function has_term() works also with arrays of categories, I have tried instead of one category, to set an array of categories in that code.条件函数has_term() 是否也适用于类别数组,我尝试代替一个类别,在该代码中设置类别数组。 But it's not working .
但它不起作用。
However, my needs have changed: I don't want to let the customer have the possibility to select cart items from different categories.但是,我的需求发生了变化:我不想让客户有可能从不同类别中选择购物车项目。 So the cart must always have items from the same product category.
因此,购物车必须始终包含来自同一产品类别的商品。
Any help please?请问有什么帮助吗?
Thanks.谢谢。
Making a custom function hooked in woocommerce_add_to_cart_validation filter hook is going to do the job in a much more simpler way, without any need to set an array of categories.在 woocommerce_add_to_cart_validation 过滤器挂钩中创建一个自定义函数将以更简单的方式完成这项工作,无需设置类别数组。
So your code will be much more faster and compact.所以你的代码会更快更紧凑。 Additionally you can display a custom notice to warn the customer.
此外,您可以显示自定义通知以警告客户。
This code will avoid adding to cart, if an item of a different category is in cart:如果购物车中有不同类别的商品,此代码将避免添加到购物车:
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );
function add_to_cart_validation_callback( $passed, $product_id, $quantity) {
// HERE set your alert text message
$message = __( 'MY ALERT MESSAGE.', 'woocommerce' );
if( ! WC()->cart->is_empty() ) {
// Get the product category terms for the current product
$terms_slugs = wp_get_post_terms( $product_id, 'product_cat' array('fields' => 'slugs'));
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ){
if( ! has_term( $terms_slugs, 'product_cat', $cart_item['product_id'] )) {
$passed = false;
wc_add_notice( $message, 'error' );
break;
}
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
This code is tested and it works这段代码已经过测试并且可以正常工作
Restricting cart items to be only from different product categories:
限制购物车项目只能来自不同的产品类别:
Replace in the function the condition:替换函数中的条件:
if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {
by经过
if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {
So, my PHP is a bit rusty from lack of use. 因此,由于缺乏使用,我的PHP有点生锈。 I need to add restrictions for example, that if there is a product already in the cart from a specific category, no other categories can be added.
我需要添加一些限制,例如,如果购物车中已经有特定类别的产品,则不能添加其他类别。 On the flip side, if products not in the restricted category are already in the cart, products from the restricted category cannot be added to the cart unless the the unrestrict category products already in cart are removed.
另一方面,如果购物车中已经存在非限制类别的产品,则除非删除了购物车中非限制类别的产品,否则不能将受限类别的产品添加到购物车中。 Will this code achieve this in some way?
这段代码会以某种方式实现吗?
Thanks in advance, Dave 预先感谢,戴夫
I just tried and i got a syntax error the array that i fixed this way:我刚刚尝试过,但我以这种方式修复的数组出现语法错误:
$terms_slugs = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
just a comma after product_cat distraction, but maybe somebody would need it.在product_cat分心之后只是一个逗号,但也许有人会需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.