Today's tutorial will bring you a step by step guide on how to create grouped product programmatically in Magento 2.
What is a grouped product? A Group Product is a collection of simple products that are presented as group. Using this, customer can purchase each product separately, or as a part of the group.
These type of products are helpful when you plan to promote cross-selling of the products. Using this type, you can combine other products with the main product.
Steps to Create Grouped Products in Magento 2 Programmatically
So, when you want to create a grouped product programmatically, here is the guide;
Step 1.
First of all, let us create a script for the same. Create grouped.php file at Magento’s root folder and add this below code:
PS It is a working code;)
<?php 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(); }
Here you go!
Based on your requirement, you are allowed to set static details. At times when you want to create multiple products programmatically with the help of a script, this script can be used and code can be customized for creating multiple products.
Step 2.
Now, execute this file at frontend side : {baseUrl}/grouped.php
Contact us if you face any issue while implementing the tutorial.