In this tutorial, I will explain to you how to create a simple product programmatically in Magento 2. Whenever we develop some functionality related to the product at that time, maybe we need to create a large number of products.
But, if we perform from the admin panel and create the product from the admin panel it takes a little bit a long time. At that time, the script will be helpful for you to create a simple product quickly.
You may also like this :
List of article to create product programmatically with different types :
- Configurable Product in Magento 2
- Grouped Product in Magento 2
- Downloadable Product in Magento 2
- Bundle Product in Magento 2
- Virtual Product in Magento 2
- How to check current area code in Magento 2
Let’s create the script for that. Create simpleproduct.php file at your Magento’s root folder (It’s working code):
<?php
/**
* Created By : Rohan Hapani
*/
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $objectManager->create('Magento\Catalog\Model\Product');
try {
$product->setName('Sample Product');
$product->setTypeId('simple');
$product->setAttributeSetId(4);
$product->setSku('sample-product');
$product->setWebsiteIds([1]);
$product->setVisibility(4);
$product->setPrice([1]);
$product->setImage('/sample/test.jpg');
$product->setSmallImage('/sample/test.jpg');
$product->setThumbnail('/sample/test.jpg');
$product->setStockData(
[
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 2,
'is_in_stock' => 1,
'qty' => 100
]
);
$product->save();
/**
* For Add Custom Options
*/
$options = [
[
"sort_order" => 1,
"title" => "Custom Option 1",
"price_type" => "fixed",
"price" => "10",
"type" => "field",
"is_require" => 0
],
[
"sort_order" => 2,
"title" => "Custom Option 2",
"price_type" => "fixed",
"price" => "20",
"type" => "field",
"is_require" => 0
]
];
foreach ($options as $customOptions) {
$product->setHasOptions(1);
$product->getResource()->save($product);
$option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($customOptions);
$option->save();
$product->addOption($option);
}
} catch (Exception $ex) {
echo $e->getMessage();
}
That’s it !!
You can set static details based on your requirements. If you want to create multiple products programmatically by the script at that time, you can use this script and customize code for multiple products create.
In this script, option array is used to add custom options in simple products. If you don’t want to add custom option, you can remove code after custom option comment line.
Now, execute this file at frontend side : {baseUrl}/simpleproduct.php
I hope this blog will helpful for easily understand how to create a simple product programmatically in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.