Magento Tutorials

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

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

5 days ago