על מנת לייצר הגבלה של רכישה רק מקטגוריה אחת (למשל כאשר כל קטגוריה מייצגת חנות או ספק נפרד) – ניתן להשתמש בקוד הבא:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
// Getting the product categories term slugs in an array for the current product
$term_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the product category of the current product don't match with a cart item
if( ! has_term( $term_slugs, 'product_cat', $cart_item['product_id'] ) ){
// Displaying a custom notice
wc_add_notice( __('ניתן להזמין רק ממבשל אחד בכל הזמנה'), 'error' );
// Avoid add to cart
return false; // exit
}
}
return $passed;
}
רפרנס:
https://stackoverflow.com/questions/52523582/allow-only-one-product-category-in-cart-at-once-in-woocommerce

