我有几个类别,我正在努力使某些类别无法与其他类别一起添加到购物车。 (添加时,应该重定向到产品并显示错误消息)。
我找到了对我有帮助的代码here,但我无法正确编辑它以便该规则适用于其他类别
问题是这样的 以下类别可以相互添加到购物车 奶酪 奶酪套装 面包 面包店 牛奶 鉴于以下类别不应与上述类别混合以及它们之间的混合 格兰普 马 食物餐车 其他 农场
来自链接的原始代码
<?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和horse的产品时不起作用?两种产品都进入购物车并且规则不起作用
<?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);
?>
帮忙解决问题
谢谢
基于上述讨论。我已经添加了逻辑。尝试下面的代码。
//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($passed_validation, $product_id) {
// Get the product categories
$product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
// Prohibited categories
$prohibited_categories = array(
'glamp',
'horse',
'foodtruck',
'other',
'farm'
);
// Allowed categories
$allowed_categories = array(
'cheese',
'cheese-sets',
'bread',
'bakery',
'milk'
);
// Check if the product is in a prohibited category
$product_is_prohibited = !empty(array_intersect($prohibited_categories, $product_categories));
// Check if the product is in an allowed category
$product_is_allowed = !empty(array_intersect($allowed_categories, $product_categories));
// Check if there are any prohibited products in the cart
$cart_has_prohibited = false;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_product_id = $cart_item['product_id'];
$cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));
if (!empty(array_intersect($prohibited_categories, $cart_product_categories))) {
$cart_has_prohibited = true;
break;
}
}
// Check if the cart has any allowed products
$cart_has_allowed = false;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_product_id = $cart_item['product_id'];
$cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));
if (!empty(array_intersect($allowed_categories, $cart_product_categories))) {
$cart_has_allowed = true;
break;
}
}
// Return true if cart is empty
if (WC()->cart->is_empty()) {
return $passed_validation;
}
// If cart contains prohibited products and the 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 review your current cart or empty your cart and try again.', 'error');
return false;
}
// If cart contains prohibited products and the product to be added is 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 review your current cart or empty your cart and try again.', 'error');
return false;
}
// If cart contains an allowed product and the product to be added is prohibited, display error message and return false.
if ($cart_has_allowed && $product_is_prohibited) {
wc_add_notice('Sorry, you can only purchase one product from the prohibited categories. Please review your current cart or empty your cart and try again.', 'error');
return false;
}
/*// If cart contains an allowed product and the product to be added is also allowed, display error message and return false.
if ($cart_has_allowed && $product_is_allowed) {
wc_add_notice('Sorry, you can only purchase one product from each category. Please review your current cart or empty your cart and try again.', 'error');
return false;
}*/
return $passed_validation;
}
add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);
Bhautik 谢谢您的帮助,您的问题和您的帮助比以往任何时候都更加激励我。我找到了另一个最终适合我的选择。
<?php
function custom_add_to_cart_validation( $passed, $product_id, $quantity ) {
// Get the product categories
$product_categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
// Creating a list of prohibited combinations of categories
$prohibited_combinations = array(
array( 'category-c', 'category-a' ),
array( 'category-c', 'category-b' ),
array( 'category-d', 'category-a' ),
array( 'category-d', 'category-b' ),
);
// Check whether the selected product is in the basket
if ( WC()->cart->get_cart_contents_count() > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_item_product_id = $cart_item['product_id'];
$cart_item_product_categories = wp_get_post_terms( $cart_item_product_id, 'product_cat', array( 'fields' => 'slugs' ) );
// Checking prohibited combinations
foreach ( $prohibited_combinations as $combination ) {
if ( in_array( $combination[0], $product_categories ) && in_array( $combination[1], $cart_item_product_categories ) ) {
// If a forbidden combination is found, we output an error message
wc_add_notice( 'You cannot add products from these categories together to the cart.', 'error' );
return false;
}
}
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3 );
?>