This tutorial is about creating a simple product in Magento 2 programmatically. In Magento 2 we can create simple products from the admin panel.
When we develop some features related to the product, then we need to create a large amount of products.
At that time, creating products from the admin panel will be a tedious process.
So with the help of this code, we can create simple products easily and quickly.
To programmatically create a Simple Product in Magento 2 follow these steps.
Step #1. Create simple_product_create.php file in the root directory of Magento
<?php
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'); // set Product Name
$product->setTypeId('simple'); // set type of product
$product->setAttributeSetId(4); // set attribute id
$product->setStatus(1); // set status 1 for enabled/ 0 for disabled
$product->setSku('sample-product'); // set sku of product
$product->setWebsiteIds(array(1)); // set website id for product
$product->setUrlKey('sample-product'); // set Url Key
$product->setMetaTitle('Sample Product'); // set Meta Title
$product->setVisibility(4);
// set visibility of product ( 1 for Not Visible Individually / 2 for Catalog / 3 for Search / 4 for Catalog, Search )
$product->setPrice(100);
$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",
"sku" => "sample-product-option-1",
"is_require" => 0
],
[
"sort_order" => 2,
"title" => "Custom Option 2",
"price_type" => "fixed",
"price" => "20",
"type" => "field",
"sku" => "sample-product-option-2",
"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);
}
echo "Product Created Successfully ";
} catch (Exception $ex) {
echo $e->getMessage();
}
|
Recommended Reading: How To Add Configurable Product In Magento 2
Step #2. Now Run the Below URL to Create a Simple Product
{BASEURL}/simple_product_create.php
We can also add a Custom option for the product programmatically.
If you don’t want to create a custom option, then remove that code and create a simple product easily.
Get custom Magento 2 development services that deliver scalable architecture and robust security—empowering your eCommerce growth with confidence.
Conclusion :
We hope the above blog helps you to clearly understand How to Create Simple Products Programmatically in Magento 2.
If you face any kind of issues with the above code implementation, you can connect with us.
Thank You.