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:
- Simple Product
- Configurable Product
- Grouped Product
- Virtual Product
- Bundle Product
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?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.
If you are a beginner in Magento 2 and have any doubts on how to create Magento 2 grouped products, feel free to post them in the Comments section below. I’d be happy to solve them.
Do share the post with fellow developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Hi Sanjay,
I’ve used your guide to create grouped products and it worked fine. However, the process is very slow: for grouped products with a few associated products, it takes many seconds to save, but when we have more than 10 associated products (up to 300 in my project), it could take 600 seconds to save one single grouped product.
Any idea how to improve the performance of this product creation?
Thanks
Hello Pau,
The improvement of performance depends on the server.
Thank you for asking.