WooCommerce Cart - Conditional Items categories validation
<p>We have an <strong>exclusive category X</strong> and others <strong>regular categories Y</strong>. What I would like:</p>

<ul>
<li>When someone orders anything from  <strong>category X</strong>, other category items cannot be added to cart and should display a warning</li>
<li><strong>category Y</strong> products should not be mixed with <strong>X</strong>.</li>
</ul>

<p>How could I achieve that?</p>

<p>I got this code from other post, but its outdated and not satisfactory: </p>

<pre><code>&lt;?php

 // Enforce single parent category items in cart at a time based on first item in cart
function get_product_top_level_category ( $product_id ) {

    $product_terms            =  get_the_terms ( $product_id, 'product_cat' );
    $product_category         =  $product_terms[0]-&gt;parent;
    $product_category_term    =  get_term ( $product_category, 'product_cat' );
    $product_category_parent  =  $product_category_term-&gt;parent;
    $product_top_category     =  $product_category_term-&gt;term_id;

    while ( $product_category_parent  !=  0 ) {
            $product_category_term    =  get_term ( $product_category_parent, 'product_cat' );
            $product_category_parent  =  $product_category_term-&gt;parent;
            $product_top_category     =  $product_category_term-&gt;term_id;
    }

    return $product_top_category;
}

add_filter ( 'woocommerce_before_cart', 'restrict_cart_to_single_category' );
function restrict_cart_to_single_category() {
    global $woocommerce;
    $cart_contents    =  $woocommerce-&gt;cart-&gt;get_cart( );
    $cart_item_keys   =  array_keys ( $cart_contents );
    $cart_item_count  =  count ( $cart_item_keys );

    // Do nothing if the cart is empty
    // Do nothing if the cart only has one item
    if ( ! $cart_contents || $cart_item_count == 1 ) {
            return null;
    }

    // Multiple Items in cart
    $first_item                    =  $cart_item_keys[0];
    $first_item_id                 =  $cart_contents[$first_item]['product_id'];
    $first_item_top_category       =  get_product_top_level_category ( $first_item_id );
    $first_item_top_category_term  =  get_term ( $first_item_top_category, 'product_cat' );
    $first_item_top_category_name  =  $first_item_top_category_term-&gt;name;

    // Now we check each subsequent items top-level parent category
    foreach ( $cart_item_keys as $key ) {
            if ( $key  ==  $first_item ) {
                    continue;
            }
            else {
                    $product_id            =  $cart_contents[$key]['product_id'];
                    $product_top_category  =  get_product_top_level_category( $product_id );

                    if ( $product_top_category  !=  $first_item_top_category ) {
                            $woocommerce-&gt;cart-&gt;set_quantity ( $key, 0, true );
                            $mismatched_categories  =  1;
                    }
            }
    }

    // we really only want to display this message once for anyone, including those that have carts already prefilled
    if ( isset ( $mismatched_categories ) ) {
            echo '&lt;p class="woocommerce-error"&gt;Only one category allowed in cart at a time.&lt;br /&gt;You are currently allowed only &lt;strong&gt;'.$first_item_top_category_name.'&lt;/strong&gt; items in your cart.&lt;br /&gt;To order a different category empty your cart first.&lt;/p&gt;';
    }
}
?&gt;
</code></pre>

<p>Thanks</p>