Php 如果购物车包含特定类别,则禁止添加到购物车(WooCommerce)
我有一个包含5个类别的商店。它是多供应商的,由于运输的复杂性,我只能销售一个特定类别(“油漆”)的产品,当它们单独在购物车中时。因此,当购物车包含绘画时,我需要防止将任何非绘画产品添加到购物车,并显示错误消息,我需要防止将绘画产品添加到包含任何其他类别的购物车,并禁用错误消息 我试图从Stackoverflow和其他地方找到的代码片段中拼凑出这段代码。逻辑似乎在我的大脑中起作用,但当我尝试实现代码(通过functions.php)时,它会阻止任何产品添加到购物车中,除非购物车是空的 这是我的密码:Php 如果购物车包含特定类别,则禁止添加到购物车(WooCommerce),php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个包含5个类别的商店。它是多供应商的,由于运输的复杂性,我只能销售一个特定类别(“油漆”)的产品,当它们单独在购物车中时。因此,当购物车包含绘画时,我需要防止将任何非绘画产品添加到购物车,并显示错误消息,我需要防止将绘画产品添加到包含任何其他类别的购物车,并禁用错误消息 我试图从Stackoverflow和其他地方找到的代码片段中拼凑出这段代码。逻辑似乎在我的大脑中起作用,但当我尝试实现代码(通过functions.php)时,它会阻止任何产品添加到购物车中,除非购物车是空的 这是我的密码
//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other() {
// Set flag false until we find a product in cat paint
$cat_check = 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 ) ) {
$cat_check = true;
// break because we only need one "true" to matter here
break;
}
}
// Return true if cart empty
if ( WC()->cart->get_cart_contents_count() == 0 ) {
return true;
}
// If cart contains paint and product to be added is not paint, display error message and return false.
elseif ( $cat_check && get_queried_object()->term_id != '245778522') {
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' );
return false;
}
// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
elseif (!$cat_check && get_queried_object()->term_id = '245778522') {
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' );
return false;
}
// Otherwise, return true.
return true;
}
add_filter( 'woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other' );
改变这个。。您的if条件需要“==”
//*** 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);
更改为此谢谢ashvek。当然,你是对的,但这并没有完全解决我的问题。根据您的更改,我现在无法将任何产品添加到包含油漆的购物车,甚至其他油漆产品,并且我可以将任何产品(包括油漆产品)添加到包含其他类别的购物车。还有其他想法吗?非常感谢你的帮助谢谢你,阿什维克。你是一位绅士,也是一位学者。写得很好:)我试图将这个答案应用到我的网站上,在那里我遇到了同样的问题,只是气体而不是油漆,我假设我只需要更改“has_term”函数,指向气体类别而不是油漆,它应该会工作吗?然而,很明显,它似乎没有
//*** 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);