{"id":842,"date":"2020-02-23T22:46:36","date_gmt":"2020-02-23T22:46:36","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/02\/23\/programmatically-create-grouped-product-in-magento-2\/"},"modified":"2025-05-22T14:55:57","modified_gmt":"2025-05-22T09:25:57","slug":"programmatically-create-grouped-product-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-create-grouped-product-in-magento-2\/","title":{"rendered":"Easy Way to Programmatically Create Grouped Product In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/types\/product-create-grouped\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Grouped Products<\/a>&nbsp;are simple individual products presented as grouped.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are&nbsp;<a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/product-create\" target=\"_blank\" rel=\"noreferrer noopener\">six product types<\/a>&nbsp;in Magento 2:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Simple Product<\/li>\n\n\n\n<li>Configurable Product<\/li>\n\n\n\n<li>Grouped Product<\/li>\n\n\n\n<li>Virtual Product<\/li>\n\n\n\n<li>Bundle Product<\/li>\n\n\n\n<li>Downloadable Product<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, each product in a group is bought as a separate item and the quantity purchased is displayed as a separate line item.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of grouped products is a computer with a mouse, keyboard, mic, and front cam.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Programmatically Create Grouped Product In Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vender]\\[Module]\\Controller\\Index;\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\Exception\\CouldNotSaveException;\nuse Magento\\Framework\\Exception\\InputException;\nuse Magento\\Framework\\Exception\\NoSuchEntityException;\nuse Magento\\Framework\\Exception\\StateException;\nuse Magento\\Framework\\View\\Asset\\Repository;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Magento\\Catalog\\Api\\ProductRepositoryInterface;\nuse Magento\\Catalog\\Api\\Data\\ProductLinkInterface;\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\GroupedProduct\\Model\\Product\\Type\\Grouped;\nuse Magento\\Catalog\\Model\\Product\\Visibility;\nuse Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status;\nclass Example extends Action\n{\n   private $pageFactory;\n   private $productRepository;\n   private $product;\n   protected $assetRepo;\n   protected $categoryLinkManagement;\n   private $productLink;\n   public function __construct(\n      Context $context,\n      PageFactory $pageFactory,\n      ProductRepositoryInterface $productRepository,\n      Product $product,\n      Repository $assetRepo,\n      ProductLinkInterface $productLink\n   ) {\n      parent::__construct($context);\n      $this->productRepository = $productRepository;\n      $this->productLink = $productLink;\n      $this->assetRepo = $assetRepo;\n      $this->product = $product;\n      $this->pageFactory = $pageFactory;\n   }\n   public function execute()\n   {\n      $this->AddGroupProduct();\n      $page = $this->pageFactory->create();\n      return $page;\n   }\n   public function AddGroupProduct()\n   {\n      try\n      {\n         $this->product->setTypeId(Grouped::TYPE_CODE)\n            ->setAttributeSetId(4)\n            ->setName('Grouped Product')\n            ->setSku('Grouped Product')\n            ->setVisibility(Visibility::VISIBILITY_BOTH)\n            ->setStatus(Status::STATUS_ENABLED)\n            ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);\n         $childrenIds = array(1, 2);\n         $associated = array();\n         $position = 0;\n         foreach ($childrenIds as $productId)\n         {\n            $position++;\n            $linkedProduct = $this->productRepository->getById($productId);\n            $this->productLink->setSku($this->product->getSku())\n               ->setLinkType('associated')\n               ->setLinkedProductSku($linkedProduct->getSku())\n               ->setLinkedProductType($linkedProduct->getTypeId())\n               ->setPosition($position)\n               ->getExtensionAttributes()\n               ->setQty(1);\n            $associated[] = $this->productLink;\n         }\n         $this->product->setProductLinks($associated);\n         $this->productRepository->save($this->product);\n      } catch (CouldNotSaveException $e)\n      {\n      } catch (InputException $e)\n      {\n      } catch (StateException $e)\n      {\n      } catch (NoSuchEntityException $e)\n      {\n      }\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 Grouped Products&nbsp;are simple individual products presented as grouped. There are&nbsp;six product types&nbsp;in Magento 2: Grouped products in Magento 2 can be a variation&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-842","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":15070,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions\/15070"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}