Allow some products to be sold only in bundles (groups) with some other products I'm making a commerce site with Drupal 8 and Drupal commerce and I have a not so common request. Some of the products should be available to buy only in groups (bundles) with some other products. E.g. if you want to buy product A in quantity 100 you must also buy product B, product C and/or product D and sum of them (B+C+D) must also be 100. Other way it shouldn't be possible to buy any of them separately.

I saw that there are some Bundle modules (not even sure if they could fulfill this request), but they exist only for Drupal 7. 

So is there some module for D8 which will allow grouping products like this. If not what would be the best way to achieve this feature?
As far as I know, there are no supported modules to acheive what you want.

So if you want a sustainable and future proof solution, you should probably code it yourself.

I would go for solution, where I create a custom entity type, Product Validation.

Here are some fields, that I think it should contain:


Product ID (The product it's active on).
Applies above (quantity) (A minimum quantity that must be purchased, before the validation is applyed).
Depending products (Products that be must purchased too).
Min SUM (The minimum sum of all the depending products).


Those are just fields that should probably be there, you can always add more fields like, if only one of depending products must be purchased, or all of them, maybe also support for multiple product ID's to avoid creating duplicates, etc.

Then add a custom validate handler, when the user clicks the place order button on the cart page.

EDIT: To alter the cart form you could use hook_form_alter, and check if (strpos($form_id, 'views_form_commerce_cart_form_default') === 0.
The cart form has a unique ID per order so that's why you want to do like that. Then you can add $form['#validate'][] = 'MY_MODULE_product_validation', where the logic will then be.

Your logic will look similar to this:

$product_validation = $entityTypeManager-&gt;getStorage('product_validation')-&gt;loadByProperties([
  'product_id' =&gt; $product-&gt;id(),
]);

if (isset($product_validation)) {
  if ($product_validation-&gt;getAppliesAbove() &gt; $product-&gt;quantity) {
    $total_quantity = 0;
    foreach ($product_validation-&gt;getDependingProducts() as $dep_product) {
      // Check if $dep_product has been added to cart.
      // If yes $total_quantity += $cart-&gt;getItem('something')-&gt;quantity.
    }
    if ($product_validation-&gt;getMinSum() &gt;= $total_quantity) {
      return TRUE;
    }
    return $form_state-&gt;setError($form, 'something');
  }
}


OBS: This code is using dummy variables and is intended to help you understand the logic, not as a "ready to implement" solution.

I hope it all makes sense.