Magento 2 Create Simple Products Programmatically

This tutorial will explain how to create simple product programmatically. Please find the below code snippet:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('test-sku');
$product->setName('Test Simple Product'); 
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setWeight(10); 
$product->setVisibility(4); // visibilty of product (1 => Not visible individually, 2 => catalog , 3 => search , 4 => catalog, search)
$product->setTaxClassId(0);
$product->setTypeId('simple'); // type of product (simple)
$product->setPrice(100);
$product->setStockData(
                        [
                            'use_config_manage_stock' => 0,
                            'manage_stock' => 1,
                            'is_in_stock' => 1,
                            'qty' => 999
                        ]
                    );
$product->save();

If you want to add an image to the product, you can get the code here on the link. 

Leave a Reply