title: Force sold individually product to be bought alone in WooCommerce
body: <p>Let's Say I have Product A which is allow to bought alone.</p>
  
  <p>If something else already in cart it shows the message. "Not Allow". I tried various methods but not able to find proper solution for that. </p>
  
  <p>When somebody tries to click on the "ADD TO CART" button then it must check via code that if other items in cart it is not allow to put in cart and shows the message.</p>
  
  <p>PRODUCT A is allow bought alone. </p>
  
  <p>I tried with the category comparison and it works. but I want to do only with the Product ID.</p>
  
  <pre class="lang-php prettyprint-override"><code>add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 5);
  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()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item) {
  
          $products_ids = 137817;
          $product = $cart_item['data'];
  
  
  
          if (has_term('miscellaneous', 'product_cat', $product-&gt;id)) {
  
  
  
              $cart_has_paint = true;
              // break because we only need one "true" to matter here
              break;
          }
  
      }
  
      $product_is_paint = false;
      if (has_term('miscellaneous', 'product_cat', $product_id)) {
  
  
  
          $product_is_paint = true;
      }
  
  // Return true if cart empty
  
      if (!WC()-&gt;cart-&gt;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 &amp;&amp; !$product_is_paint) {
               echo '&lt;script type="text/javascript"&gt;';
              echo ' alert("Hello,Sorry, “custom order” items must be purchased separately! To purchase this item, please either checkout with your current cart or remove any “custom order” items from your cart to enable you add the regular items.")';  //not showing an alert box.
              echo '&lt;/script&gt;';
  
  
              $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 &amp;&amp; $product_is_paint) {
              echo '&lt;script type="text/javascript"&gt;';
              echo ' alert("Sorry, “custom order” item must be purchased separately! To purchase any “custom order” items, please either checkout with your current cart or empty cart to enable you add the “custom order” items.")';  //not showing an alert box.
              echo '&lt;/script&gt;';
  
  
              $validation = false;
          }
      }
      // Otherwise, return true.
      return $validation;
  }
  </code></pre>
  
  <p>This code only works with the <code>miscellaneous</code> category I want to allow only with the Product ID.. not category..</p>