title: Allow only one product per product category in cart
body: <p>On this <a href="https://stackoverflow.com/questions/42400501/restricting-cart-items-to-be-from-the-same-product-category"><strong>Question / Answer</strong></a> I have found the PHP code about how to just add one product per category in cart in Woocommerce. </p>
  
  <p>The code works just fine, but I want to add the latest added product to cart and if there is already a product of that category in the cart, I want to have the oldest deleted. </p>
  
  <pre><code>add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
  function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
  
  // HERE Type your alert displayed message
  // $message = __( 'BLA BLA YOUR MESSAGE.', 'woocommerce' );
  
  $product_cats_object = get_the_terms( $product_id, 'product_cat' );
  foreach($product_cats_object as $obj_prod_cat){
      $product_cats[] = $obj_prod_cat-&gt;slug;
  }
  
  foreach (WC()-&gt;cart-&gt;get_cart() as $cart_item ){
  if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {
          $passed = false;
          wc_add_notice( $message, 'error' );
          break;
      }
  }
  return $passed;
  }
  </code></pre>
  
  <p>It is a piece of code that comes from <a href="https://stackoverflow.com/users/3730754/loictheaztec"><strong>LoicTheAztec</strong> </a>. It works fine but I need an extra option...</p>
  
  <p>In my Woocommerce there are 5 different categories, there is just place in the cart for one item per category. The latest added item should stay, the item of the same category that is already in cart must be removed from cart. 
  Does someone of you has a solution? </p>
  
  <p>Many thanks!</p>