How to Create a Simple Product Programmatically in Magento 2
While development or testing, you may need a huge amount of products. Magento 2 has the feature to create the products from the backend but there is the time at which you want to create multiple products to test page performance, you need to create products in a certain format, you have some custom attribute or you have created your own attribute set or product type. It’s when you need to create products programmatically to ease the task and save the time. Here, I’ve come up with the custom code to implement to create a simple product programmatically in Magento 2!
There are six product types in Magento 2:
- Simple Product
- Configurable Product
- Grouped Product
- Virtual Product
- Bundle Product
- Downloadable Product
The below method allows creating simple product programmatically along with custom options. Moreover, you can add images to the media gallery of the product with the below method.
Create a Simple Product Programmatically in Magento 2:
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 |
<?php use Magento\Framework\App\Bootstrap; require 'app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $product = $objectManager->create('Magento\Catalog\Model\Product'); try { $product->setName('Test Product'); $product->setTypeId('simple'); $product->setAttributeSetId(4); $product->setSku('test-SKU'); $product->setWebsiteIds(array(1)); $product->setVisibility(4); $product->setPrice(array(1)); $product->setImage('/testimg/test.jpg'); $product->setSmallImage('/testimg/test.jpg'); $product->setThumbnail('/testimg/test.jpg'); $product->setStockData(array( 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'min_sale_qty' => 1, 'max_sale_qty' => 2, 'is_in_stock' => 1, 'qty' => 100 ) ); $product->save(); // Adding Custom option to product $options = array( array( "sort_order" => 1, "title" => "Custom Option 1", "price_type" => "fixed", "price" => "10", "type" => "field", "is_require" => 0 ), array( "sort_order" => 2, "title" => "Custom Option 2", "price_type" => "fixed", "price" => "20", "type" => "field", "is_require" => 0 ) ); foreach ($options as $arrayOption) { $product->setHasOptions(1); $product->getResource()->save($product); $option = $objectManager->create('\Magento\Catalog\Model\Product\Option') ->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($arrayOption); $option->save(); $product->addOption($option); } } catch (\Exception $e) { echo $e->getMessage(); } |
That’s it and we have created a simple product quickly! If you still can’t see it on the frontend, reindex and clear your cache.
This is the easiest method possible, however, if you get stuck in the implementation, please mention it in the comments section below and I’d be happy to help!
If you opt to create simple product in Magento 2 from the admin panel, refer our blog post.
Flash 5 stars for the post if found useful?
Happy Coding!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
3 Comments
Your not wrong, whats happening is that while your code works once, try running it again setting the SKU to a different value, while every other value such as name and description is correctly changing the setSku is retained. for example my first itteration i setSku(‘product1’) second time in my loop i setSku(‘product2’) but if i do a getSku on the product it shows “product1”
I used to use code like this to create multiple products into my 2.2 site, but as of upgrading to 2.3 the code hangs on the second product_>save itteration. Not sure why as yet.
Dominic, the current code works well for Magento 2.3. It seems like your catalog product table is having duplicate entry of SKU or URL key and that’s why showing this issue.