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
- Configurable Product
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<?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.
1 |
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