In this tutorial, I will explain to how to create grouped product programmatically in Magento 2. A Group Product is collection of simple products that are presented as group. Using this Customer can purchase each product separately, or as the part of the group.
This type of product will helpful when you want to promoting your products. Using this type, you can combine other products also with main product.
You may also like this :
List of article to create product programmatically with different types :
- Simple Product Programmatically in Magento 2
- Configurable Product Programmatically in Magento 2
- Downloadable Product Programmatically in Magento 2
- Bundle Product Programmatically in Magento 2
- Virtual Product Programmatically in Magento 2
So, When you want to create grouped product programmatically then, how can you create? Let’s now create script for that. Create grouped.php file at Magento’s root folder and add this below code (It’s Working Code) :
<?php
/**
* Created By : Rohan Hapani
*/
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $objectManager->create('Magento\Catalog\Model\Product');
try {
$product->setName('Grouped Product'); // Set Product Name
$product->setTypeId('grouped'); // Set Product Type Id
$product->setAttributeSetId(11); // Set Attribute Set ID
$product->setSku('grouped-product'); // Set SKU
$product->setWebsiteIds([1]); // Set Website Ids
$product->setVisibility(4); // Set Visibility
$product->setPrice([1]); // Set Price
$product->setImage('/grouped/test.jpg'); // Image Path
$product->setSmallImage('/grouped/test.jpg'); // Small Image Path
$product->setThumbnail('/grouped/test.jpg'); // Thumbnail Image Path
$product->setStockData(
[
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 2,
'is_in_stock' => 1,
'qty' => 100,
]
);
$product->save();
$childrenIds = [2, 3, 4]; // Set Associated Product ids
$associated = [];
$position = 0;
foreach ($childrenIds as $productId)
{
$position++;
$linkedProduct = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
$productLink = $objectManager->create('\Magento\Catalog\Api\Data\ProductLinkInterface');
$productLink->setSku($product->getSku())
->setLinkType('associated') // Set Link Type
->setLinkedProductSku($linkedProduct->getSku()) // Set Link Product SKU
->setLinkedProductType($linkedProduct->getTypeId()) // Set Link Product Type
->setPosition($position) // Set Position
->getExtensionAttributes()
->setQty(1);
// Set Associated Product Default QTY
$associated[] = $productLink;
}
$product->setProductLinks($associated);
$product->save();
if ($product->getId())
{
echo "Product Created Successfully";
}
} catch (\Exception $e) {
echo $e->getMessage();
}
That’s it !!
You can set static details based on your requirements. If you want to create multiple products programmatically by the script at that time, you can use this script and customize code for multiple products create.
Now, execute this file at frontend side : {baseUrl}/grouped.php
I hope this blog will helpful for easily understand how to create grouped product programmatically in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.