{"id":1455,"date":"2017-03-30T10:08:20","date_gmt":"2017-03-30T04:38:20","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=1455"},"modified":"2020-03-13T16:03:47","modified_gmt":"2020-03-13T10:33:47","slug":"magento-2-create-simple-product-installdata","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-simple-product-installdata\/","title":{"rendered":"Magento 2 &#8211; How to create a simple product programmatically using InstallData?"},"content":{"rendered":"<p>You can create simple product programmatically using InstallData by using the below InstallData script.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Vendor\\Product\\Setup;\r\n\r\nuse Magento\\Framework\\Setup\\InstallDataInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\n\r\nclass InstallData implements InstallDataInterface\r\n{\r\n    \/**\r\n     * @var string\r\n     *\/\r\n    protected $productType = \\Magento\\Catalog\\Model\\Product\\Type::TYPE_SIMPLE;\r\n    \/**\r\n     * @var \\Magento\\Catalog\\Model\\ProductFactory\r\n     *\/\r\n    protected $productFactory;\r\n\r\n    \/**\r\n     * @var \\Magento\\Store\\Model\\StoreManagerInterface\r\n     *\/\r\n    protected $storeManager;\r\n\r\n    public function __construct(\r\n        \\Magento\\Catalog\\Model\\ProductFactory $productFactory,\r\n        \\Magento\\Store\\Model\\StoreManagerInterface $storeManager\r\n    )\r\n    {\r\n        $this-&gt;productFactory = $productFactory;\r\n        $this-&gt;storeManager = $storeManager;\r\n    }\r\n\r\n    \/**\r\n     * {@inheritdoc}\r\n     * @SuppressWarnings(PHPMD.CyclomaticComplexity)\r\n     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)\r\n     * @SuppressWarnings(PHPMD.NPathComplexity)\r\n     *\/\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\r\n    {\r\n        $data = [\r\n            'name' =&gt; 'Test',\r\n            'sku'  =&gt; 'sku001',\r\n            'price' =&gt; 456,\r\n            'weight' =&gt; 1\r\n        ];\r\n        $attributeSetId = 4; \/\/Attribute set default\r\n        $product = $this-&gt;productFactory-&gt;create();\r\n        $product-&gt;setData($data);\r\n        $product\r\n            -&gt;setTypeId($this-&gt;productType)\r\n            -&gt;setAttributeSetId($attributeSetId)\r\n            -&gt;setWebsiteIds([$this-&gt;storeManager-&gt;getDefaultStoreView()-&gt;getWebsiteId()])\r\n            -&gt;setStatus(\\Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status::STATUS_ENABLED)\r\n            -&gt;setStockData(['is_in_stock' =&gt; 1, 'manage_stock' =&gt; 0, 'qty' =&gt; 100])\r\n            -&gt;setStoreId(\\Magento\\Store\\Model\\Store::DEFAULT_STORE_ID);\r\n\r\n        if (empty($data['visibility'])) {\r\n            $product-&gt;setVisibility(\\Magento\\Catalog\\Model\\Product\\Visibility::VISIBILITY_BOTH);\r\n        }\r\n        $product-&gt;save();\r\n    }\r\n}<\/pre>\n<p>Ref: http:\/\/magento.stackexchange.com\/questions\/166739\/magento-2-how-to-create-simple-product-by-upgradeschema<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create simple product programmatically using InstallData by using the below InstallData script. &lt;?php namespace Vendor\\Product\\Setup; use Magento\\Framework\\Setup\\InstallDataInterface; use Magento\\Framework\\Setup\\ModuleContextInterface; use Magento\\Framework\\Setup\\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { \/** * @var string *\/ protected $productType = \\Magento\\Catalog\\Model\\Product\\Type::TYPE_SIMPLE; \/** * @var \\Magento\\Catalog\\Model\\ProductFactory *\/ protected $productFactory; \/** * @var \\Magento\\Store\\Model\\StoreManagerInterface *\/ protected $storeManager; public function __construct( \\Magento\\Catalog\\Model\\ProductFactory&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-simple-product-installdata\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4742,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[192,193,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/1455"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=1455"}],"version-history":[{"count":4,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/1455\/revisions"}],"predecessor-version":[{"id":10539,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/1455\/revisions\/10539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4742"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=1455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=1455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=1455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}