How to Create Grouped Product Programmatically in Magento 2

Hello Magento Friends,

You all have already learned about creating a simple product and configurable product programmatically in Magento 2. Today let’s learn about, How to Create Grouped Product Programmatically in Magento 2.

Mainly we can create 6 types of products Programmatically in Magento 2:

A grouped product is the combination of multiple simple products where the customers can buy all the products together or separately. Grouped products are used for cross-selling to provide additional options for buying. Example of grouped products includes blow dryer with a comb or a mobile phone with a case.

Now you know all about grouped products, you require to create grouped products for your Magento 2 store. Doing the task manually is very time-consuming. So, let us learn How to Create Grouped Product Programmatically in Magento 2.

Steps to Create Grouped Product Programmatically in Magento 2:

Step 1: Create a group_product.php file in the Magento root directory and add the below code.

<?php

use Magento\Framework\AppInterface;

try {

    require_once __DIR__ . '/app/bootstrap.php';

} catch (\Exception $e) {

    echo 'Autoload error: ' . $e->getMessage();

    exit(1);

}

try {

    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();

    $appState = $objectManager->get('Magento\Framework\App\State');

    $appState->setAreaCode('frontend');




    $grouped_product = $objectManager->create('Magento\Catalog\Model\Product');

    $grouped_product->setName('Grouped Product Name'); // Set Your Grouped Product Name

    $grouped_product->setSku('grouped_product_sku'); // Set Grouped Product SKU

    $grouped_product->setTypeId('grouped'); // Set Product Type Id (simple/virtual/downloadable/configurable/grouped/bundle)

    $grouped_product->setAttributeSetId(4); // set Attribute Id

    $grouped_product->setStatus(1);  // status enabled/disabled 1/0

    $grouped_product->setWebsiteIds(array(1)); // Set Website Ids

    $grouped_product->setVisibility(4); // visibility of product (Not Visible Individually (1) / Catalog (2)/ Search (3)/ Catalog, Search(4))

    $category_id = array(2, 3);

    $grouped_product->setCategoryIds($category_id);

    $grouped_product->setStockData(

        [

            'use_config_manage_stock' => 0,

            'manage_stock' => 1,

            'min_sale_qty' => 1,

            'max_sale_qty' => 2,

            'is_in_stock' => 1,

            'qty' => 1000,

        ]

    );

    $grouped_product->save();

    $associated_id = [1, 9]; // Set Your Associated Product ids here

    $associated_array = [];

    $associated_product_position = 0;

    foreach ($associated_id as $product_id)

    {

        $associated_product_position++;

        $product_repository_interface = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->getById($product_id);

        $product_link_interface = $objectManager->create('\Magento\Catalog\Api\Data\ProductLinkInterface');

        $product_link_interface->setSku($grouped_product->getSku())

            ->setLinkType('associated')

            ->setLinkedProductSku($product_repository_interface->getSku())

            ->setLinkedProductType($product_repository_interface->getTypeId())

            ->setPosition($associated_product_position)

            ->getExtensionAttributes()

            ->setQty(1);

        $associated_array[] = $product_link_interface;

    }

    $grouped_product->setProductLinks($associated_array);

    $grouped_product->save();

    if ($grouped_product->getId())

    {

        echo "Grouped Product Created Successfully";

    }

} catch (\Exception $e) {

    echo $e->getMessage();

}

After adding the above code, run the below URL.

https://yourdomain.com/group_product.php

That’s it!

Conclusion:

Hence, this way you can Create Grouped Product Programmatically in Magento 2. Want to avoid all distress? Hire our Certified Magento Developer that will do all the work for you. In case of any difficulty, you can contact me by commenting below. If you found the solution useful, hit 5 stars, share it with your friends, and stay in touch with us!

Happy Reading

Previous Article

How to Create Configurable Product Programmatically in Magento 2

Next Article

How to Create Virtual Product Programmatically in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨