This tutorial is about to Create Simple Product in Magento 2 programmatically. In Magento 2 we can create simple product from Admin panel.
When we develop some features related to the product, then we need to create large amount of products. At that time, creating products from 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 Simple Product in Magento 2 follow the below 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(); } |
Also Read:- 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.
Conclusion :
We hope the above blog helps you to clearly understand How to Create Simple Product Programmatically in Magento 2.
In case of any kind of problem with the above code implementation, you can contact us or let us know in the comment section.
Thank You.