With Grouped Product in Magento 2, merchants can grab multiple items on a single page that simplify the customer shopping experience. Each product detail page displays many single products collected by season or theme that you can promote sales for multiple items at the same time.

 

For example, you sell wedding dresses and you can easily add the accessories that come with the dress such as necklaces, earrings, handbags, glasses, etc. Presenting a grouped product can create an incentive for customers to purchase additional items.

 

Although they are presented as a group, each product in the group is purchased as a separate item. In the shopping cart, each item and the quantity purchased appears a separate line item.

 

Actually, Grouped Product is considered as the most complex product type in Magento platform.

 

So how can you do that with programmatically?

 

First, you need to create a product with type 'grouped'. You can refer to the code below:

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('my-sku'); // Set your sku here
$product->setName('Sample Simple Product'); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibility of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('grouped'); // type of product (simple/virtual/downloadable/configurable/grouped)
$product->setPrice(100); // price of product
$product->setStockData(
   array(
       'use_config_manage_stock' => 0,
       'manage_stock' => 1,
       'is_in_stock' => 1,
       'qty' => 999999999
   )
);

*We have set the type of product is 'grouped'.

 

Grouped Product is added to link as the related product are linked, below is the code that you need to add before saving the product.

 

For example, we will retrieve 3 products with ids of 1, 2 and 3 respectively to add to link with group product. And id of group product is 4. 

 

We will get group product with id use:

 

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $this->_productRepository;
$product = $productRepository->getById(4); //get product group

 

Them, we will browse each id in the array $childrenIds to then retrieve each product data according to that id. After that, we use:

 

 /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
   $productLink = $this->_productLinkInterface->create();

for create product link.

 

Next step, we set the data for product link and save it into the array $associated.

 

Finally, using function setProductLinks() to set array $associated for product group.

 

Below is the full code for adding products to group product:

 

$childrenIds = array(1,2,3);
$associated = array();
$position = 0;
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $this->_productRepository;
$product = $productRepository->getById(4); //get product group
foreach($childrenIds as $productId){
   $position++;
   //You need to load each product to get what you need in order to build $productLink
   $linkedProduct = $productRepository->getById($productId);
   /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
   $productLink = $this->_productLinkInterface->create();
   $productLink->setSku($product->getSku()) //sku of product group
       ->setLinkType('associated')
       ->setLinkedProductSku($linkedProduct->getSku())
       ->setLinkedProductType($linkedProduct->getTypeId())
       ->setPosition($position)
       ->getExtensionAttributes()
       ->setQty(1);
   $associated[] = $productLink;
}
$product->setProductLinks($associated);
$productRepository->save($product);

 

So, creating a group product with programmatically will be simple when you follow the above instruction. However, Magento default only allows adding simple products without custom options to a grouped product, so that is one of the drawbacks of this product type.