Easy Way to Programmatically Create Grouped Product In Magento 2

Magento 2 Grouped Products are simple individual products presented as grouped.

There are six product types in Magento 2:

  1. Simple Product
  2. Configurable Product
  3. Grouped Product
  4. Virtual Product
  5. Bundle Product
  6. Downloadable Product

Grouped products in Magento 2 can be a variation of a single product or group them by theme. Doing so encourages the customers to buy additional items too. Offer variations of a product, and offer them all on a single page with the grouped products.

However, each product in a group is bought as a separate item and the quantity purchased is displayed as a separate line item.

An example of grouped products is a computer with a mouse, keyboard, mic, and front cam.

Method To Programmatically Create Grouped Product In Magento 2:

<?php
namespace [Vender]\[Module]\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Result\PageFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductLinkInterface;
use Magento\Catalog\Model\Product;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
class Example extends Action
{
   private $pageFactory;
   private $productRepository;
   private $product;
   protected $assetRepo;
   protected $categoryLinkManagement;
   private $productLink;
   public function __construct(
      Context $context,
      PageFactory $pageFactory,
      ProductRepositoryInterface $productRepository,
      Product $product,
      Repository $assetRepo,
      ProductLinkInterface $productLink
   ) {
      parent::__construct($context);
      $this->productRepository = $productRepository;
      $this->productLink = $productLink;
      $this->assetRepo = $assetRepo;
      $this->product = $product;
      $this->pageFactory = $pageFactory;
   }
   public function execute()
   {
      $this->AddGroupProduct();
      $page = $this->pageFactory->create();
      return $page;
   }
   public function AddGroupProduct()
   {
      try
      {
         $this->product->setTypeId(Grouped::TYPE_CODE)
            ->setAttributeSetId(4)
            ->setName('Grouped Product')
            ->setSku('Grouped Product')
            ->setVisibility(Visibility::VISIBILITY_BOTH)
            ->setStatus(Status::STATUS_ENABLED)
            ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
         $childrenIds = array(1, 2);
         $associated = array();
         $position = 0;
         foreach ($childrenIds as $productId)
         {
            $position++;
            $linkedProduct = $this->productRepository->getById($productId);
            $this->productLink->setSku($this->product->getSku())
               ->setLinkType('associated')
               ->setLinkedProductSku($linkedProduct->getSku())
               ->setLinkedProductType($linkedProduct->getTypeId())
               ->setPosition($position)
               ->getExtensionAttributes()
               ->setQty(1);
            $associated[] = $this->productLink;
         }
         $this->product->setProductLinks($associated);
         $this->productRepository->save($this->product);
      } catch (CouldNotSaveException $e)
      {
      } catch (InputException $e)
      {
      } catch (StateException $e)
      {
      } catch (NoSuchEntityException $e)
      {
      }
   }
}

That’s it.

Do share the post with fellow developers via social media.

Thank you.

Table of Contents