我有几个类别,我正试图使它,使一些类别不能被添加到购物沿着其他人。(添加时,应有一个指向产品的重定向,并显示错误消息)。
我找到了帮助我here的代码,但我无法正确编辑它,使规则适用于其他类别
问题是这样的下面的类别可以添加到购物车之间的奶酪奶酪集面包面包店牛奶而下面的类别不应该与上面的类别和之间的glamp马foodtruck其他农场混合
来自链路的原始代码
<?php
//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other($validation, $product_id) {
// Set flag false until we find a product in cat paint
$cart_has_paint = false;
// Set $cat_check true if a cart item is in paint cat
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
if (has_term('paint', 'product_cat', $product->id)) {
$cart_has_paint = true;
// break because we only need one "true" to matter here
break;
}
}
$product_is_paint = false;
if (has_term('paint', 'product_cat', $product_id)) {
$product_is_paint = true;
}
// Return true if cart empty
if (!WC()->cart->get_cart_contents_count() == 0) {
// If cart contains paint and product to be added is not paint, display error message and return false.
if ($cart_has_paint && !$product_is_paint) {
wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
$validation = false;
}
// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
elseif (!$cart_has_paint && $product_is_paint) {
wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
$validation = false;
}
}
// Otherwise, return true.
return $validation;
}
add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 2);
?>
字符串
和返工,但不工作^)当“允许”类别在购物车,我添加任何“禁止”类别其工作,但当我尝试添加产品从glamp和马不工作?两个产品都去购物车和d规则不工作
<?php
//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($validation, $product_id) {
// Set flag false until we find a product in prohibited categories
$cart_has_prohibited = false;
// Prohibited categories
$prohibited_categories = array(
'glamp',
'horse',
'foodtruck',
'other',
'farm'
);
// Allowed categories
$allowed_categories = array(
'cheese',
'cheese-sets',
'bread',
'bakery',
'milk'
);
// Set $cat_check true if a cart item is in prohibited categories
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$product_categories = wp_get_post_terms($product->id, 'product_cat', array("fields" => "slugs"));
$intersect_categories = array_intersect($prohibited_categories, $product_categories);
if (!empty($intersect_categories)) {
$cart_has_prohibited = true;
// break because we only need one "true" to matter here
break;
}
}
$product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
$intersect_categories = array_intersect($prohibited_categories, $product_categories);
$product_is_prohibited = !empty($intersect_categories);
// Return true if cart empty
if (!WC()->cart->is_empty()) {
// If cart contains prohibited categories and product to be added is not prohibited, display error message and return false.
if ($cart_has_prohibited && !$product_is_prohibited) {
wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
$validation = false;
}
// If cart contains a product that is not prohibited and product to be added is from the prohibited categories, display error message and return false.
elseif (!$cart_has_prohibited && $product_is_prohibited) {
wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
$validation = false;
}
}
// Otherwise, return true.
return $validation;
}
add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);
?>
型
帮助解决问题
谢啦,谢啦
2条答案
按热度按时间mnemlml81#
基于上述讨论。我添加了逻辑。试试下面的代码。
字符串
34gzjxbg2#
Bhautik感谢你的帮助,你的问题和你的帮助比以往任何时候都更激励我。我找到了另一个最终适合我的选择。
字符串