{"id":161,"date":"2018-08-23T13:00:14","date_gmt":"2018-08-23T13:00:14","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/08\/23\/create-a-simple-product-programmatically-in-magento-2\/"},"modified":"2025-05-22T17:21:45","modified_gmt":"2025-05-22T11:51:45","slug":"create-a-simple-product-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-a-simple-product-programmatically-in-magento-2\/","title":{"rendered":"How to Create a Simple Product Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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\u2019s when you need to create products programmatically to ease the task and save the time. Here, I\u2019ve come up with the custom code to implement to&nbsp;<em><strong>create a simple product programmatically in Magento 2<\/strong><\/em>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are&nbsp;<a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/product-create\" target=\"_blank\" rel=\"noreferrer noopener\">six product types<\/a>&nbsp;in Magento 2:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Simple Product<\/li>\n\n\n\n<li>Configurable Product<\/li>\n\n\n\n<li>Grouped Product<\/li>\n\n\n\n<li>Virtual Product<\/li>\n\n\n\n<li>Bundle Product<\/li>\n\n\n\n<li>Downloadable Product<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The below method allows creating simple product programmatically along with custom options. Moreover, you can add images to the media gallery&nbsp;of the product with the below method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Simple Product Programmatically in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nuse Magento\\Framework\\App\\Bootstrap;\n \nrequire 'app\/bootstrap.php';\n$bootstrap = Bootstrap::create(BP, $_SERVER);\n$objectManager = $bootstrap->getObjectManager();\n$state = $objectManager->get('\\Magento\\Framework\\App\\State');\n$state->setAreaCode('frontend');\n \n$product = $objectManager->create('Magento\\Catalog\\Model\\Product');\n \ntry {\n    $product->setName('Test Product');\n    $product->setTypeId('simple');\n    $product->setAttributeSetId(4);\n    $product->setSku('test-SKU');\n    $product->setWebsiteIds(array(1));\n    $product->setVisibility(4);\n    $product->setPrice(array(1));\n    $product->setImage('\/testimg\/test.jpg');\n    $product->setSmallImage('\/testimg\/test.jpg');\n    $product->setThumbnail('\/testimg\/test.jpg');\n    $product->setStockData(array(\n            'use_config_manage_stock' => 0,\n            'manage_stock' => 1,\n            'min_sale_qty' => 1,\n            'max_sale_qty' => 2,\n            'is_in_stock' => 1,\n            'qty' => 100\n        )\n    );\n \n    $product->save();\n \n    \/\/ Adding Custom option to product\n    $options = array(\n        array(\n            \"sort_order\" => 1,\n            \"title\" => \"Custom Option 1\",\n            \"price_type\" => \"fixed\",\n            \"price\" => \"10\",\n            \"type\" => \"field\",\n            \"is_require\" => 0\n        ),\n        array(\n            \"sort_order\" => 2,\n            \"title\" => \"Custom Option 2\",\n            \"price_type\" => \"fixed\",\n            \"price\" => \"20\",\n            \"type\" => \"field\",\n            \"is_require\" => 0\n        )\n    );\n    foreach ($options as $arrayOption) {\n        $product->setHasOptions(1);\n        $product->getResource()->save($product);\n        $option = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Option')\n            ->setProductId($product->getId())\n            ->setStoreId($product->getStoreId())\n            ->addData($arrayOption);\n        $option->save();\n        $product->addOption($option);\n    }\n} catch (\\Exception $e) {\n    echo $e->getMessage();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it and we have created a simple product quickly! If you still can\u2019t see it on the frontend, reindex and clear your cache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you opt to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-simple-product-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create simple product in Magento 2<\/a>&nbsp;from the admin panel, refer our blog post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":15617,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/161\/revisions\/15617"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}