---
title: "How to Create Grouped Product Programmatically in Magento 2?"
url: "https://magecomp.com/blog/create-grouped-product-programmatically-magento-2/"
date: "2021-04-07T09:10:18+00:00"
modified: "2025-01-28T11:51:56+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "Magento Tutorials"
word_count: 541
reading_time: "3 min read"
summary: "Hello Magento Friends,"
description: "Magento facilitates creating various types of products. Grouped products are used for cross-selling. Learn How to Create Grouped Products Programmatically in..."
keywords: "Magento Tutorials"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to send Newsletters Manually to Customer Group in Magento 2"
    url: "https://magecomp.com/blog/send-newsletters-manually-to-customer-group-in-magento-2/"
  - title: "How to Change Magento 2 Admin URL? (3 Methods)"
    url: "https://magecomp.com/blog/change-magento-2-admin-url-3-methods/"
  - title: "How to Add Custom Notice to Page Top in Magento 2?"
    url: "https://magecomp.com/blog/add-custom-notice-to-page-top-in-magento-2/"
---

# How to Create Grouped Product Programmatically in Magento 2?

_Published: April 7, 2021_  
_Author: Dhiren Vasoya_  

![How-to-Create-Grouped-Product-Programmatically-in-Magento-2](https://magecomp.com/blog/wp-content/uploads/2021/04/How-to-Create-Grouped-Product-Programmatically-in-Magento-2.png)

**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:
- **[Simple Product](https://magecomp.com/blog/create-simple-product-programmatically-in-magento-2/)**
- **[Configurable Product](https://magecomp.com/blog/create-configurable-product-programmatically-in-magento-2/)**
- **Grouped Product**
- **Virtual Product**
- **Bundle Product**
- **Downloadable Product**

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 MagentoFrameworkAppInterface;

try {

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

} catch (Exception $e) {

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

    exit(1);

}

try {

    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();

    $appState = $objectManager->get('MagentoFrameworkAppState');

    $appState->setAreaCode('frontend');

    $grouped_product = $objectManager->create('MagentoCatalogModelProduct');

    $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('MagentoCatalogApiProductRepositoryInterface')->getById($product_id);

        $product_link_interface = $objectManager->create('MagentoCatalogApiDataProductLinkInterface');

        $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**](https://magecomp.com/hire-magento-developer.html) 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**


---

_View the original post at: [https://magecomp.com/blog/create-grouped-product-programmatically-magento-2/](https://magecomp.com/blog/create-grouped-product-programmatically-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-15 18:03:34 UTC_  
