title: Restricting cart items to be from the same product category in WooCommerce
body: <p>I am using the code below to remove other WooCommerce product category items from the cart when there is an item with a special product category 'cat_x' added in cart and display some different custom notices. The code came from <a href="https://stackoverflow.com/questions/38390058/woocommerce-cart-conditional-items-categories-validation"><strong>this thread</strong></a> and just works well:</p>
  
  <pre><code>add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );
  function checking_cart_items() {
      $special = false;
      $catx = 'cat_x';
      $number_of_items = sizeof( WC()-&gt;cart-&gt;get_cart() );
  
      if ( $number_of_items &gt; 0 ) {
  
          // Loop through all cart products
          foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
              $item = $values['data'];
              $item_id = $item-&gt;id;
  
              // detecting if 'cat_x' item is in cart
              if ( has_term( $catx, 'product_cat', $item_id ) ) {
                  if (!$special)
                      $special = true;
              }
          }
  
          // Re-loop through all cart products
          foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
              $item = $values['data'];
              $item_id = $item-&gt;id;
  
              if ( $special ) // there is a 'cat_x' item in cart
              { 
                  if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart
                      if ( empty( $notice ) )
                          $notice = '1';
                  }
                  if ( $number_of_items &gt;= 2 ) { // 'cat_x' item + other categories items in cart
              // removing other categories items from cart
                      if ( !has_term( $catx, 'product_cat', $item_id ) ) {
                          WC()-&gt;cart-&gt;remove_cart_item( $cart_item_key ); // removing item from cart
                          if ( empty( $notice ) || $notice == '1' )
                              $notice = '2';
                      }
                  }
              } else { // Only other categories items
                  if ( empty( $notice ) )
                      $notice = '3';
              }
          }
  
          // Firing notices
          if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)
              wc_add_notice( sprintf( '&lt;p class="woocommerce-error"&gt;bla bla bla one category X item in the cart&lt;/p&gt;' ), 'success' );
          } elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones =&gt; removed other ones 
              wc_add_notice( sprintf( '&lt;p class="woocommerce-error"&gt;bla bla bla ther is already category X in the cart =&gt; Other category items has been removed&lt;/p&gt;' ), 'error' );
          } elseif ( $notice == '3' ) { // message for other categories items (if needed)
              wc_add_notice( sprintf( '&lt;p class="woocommerce-error"&gt;bla bla bla NOT category X in the cart&lt;/p&gt;' ), 'success' );
          }
      }
  } 
  </code></pre>
  
  <p>Has the conditional function <strong>has_term()</strong> works also with arrays of categories, I have tried instead of one category, to set an array of categories in that code. <strong>But it’s not working</strong>.</p>
  
  <p>However, my needs have changed: I don’t want to let the customer have the possibility to select cart items from different categories. So the cart must always have items from the same product category.</p>
  
  <p>Any help please?</p>
  
  <p>Thanks.</p>