To create a product by installer you need to add file “InstallData.php” at path:
app/code/Webkul/Hello/Setup/InstallData.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php namespace Webkul\Hello\Setup; use Magento\Framework\Setup; class InstallData implements Setup\InstallDataInterface { protected $storeManager; protected $productFactory; protected $productModel; protected $_objectManager; protected $productType = \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL; //set the type of product public function __construct( \Magento\Catalog\Model\Product $productModel, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Catalog\Model\ProductFactory $productFactory ) { $this->productModel = $productModel; $this->storeManager = $storeManager; $this->_objectManager = $objectManager; $this->productFactory = $productFactory; } public function install(Setup\ModuleDataSetupInterface $setup, Setup\ModuleContextInterface $moduleContext) { $appState = $this->_objectManager->get('Magento\Framework\App\State'); $appState->setAreaCode('adminmhtml'); $product = $this->productFactory->create(); $attributeSetId = $this->productModel->getDefaultAttributeSetId(); $product->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); $product->setWebsiteIds([$this->storeManager->getDefaultStoreView()->getWebsiteId()]); $product->setTypeId($this->productType); $product->addData(array( 'name' => 'Sample product',//name of product 'attribute_set_id' => $attributeSetId, 'status' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED, 'visibility' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE, 'weight' => 1, 'sku' => 'sample_product',//SKU of product 'tax_class_id' => 0, 'description' => 'sample_product', 'short_description' => 'sample_product', 'stock_data' => array( //stock management 'manage_stock' => 1, 'qty' => 999, 'is_in_stock' => 1 ) )); $product->save(); } } |
Here,
1 |
\Magento\Catalog\Model\Product\Type : is used to access type of product. |
1 |
$this->productModel->getDefaultAttributeSetId(): used to get get default attribute set id |
1 |
\Magento\Catalog\Model\Product\Visibility : used to get visibility of product |
1 |
\Magento\Catalog\Model\Product\Attribute\Source\Status: used to get status of product |
1 |
$this->storeManager->getDefaultStoreView()->getWebsiteId(): used to get website id. |
1 |
\Magento\Store\Model\Store: used to get store id |
1 |
setAreaCode('adminmhtml'): used to set "admin" as an area code. |
After that, you just need to run upgrade command, and your product is created in your site.
Upgrade command: php bin/magento setup:upgrade
I nice article, I have once concern regarding create custom attribute, suppose I have created one product attribute and want to assign to attribute set say Bike (Other than Default), How can I do that ? By default Magento 2 assign attribute to all available attribute set say Default and Bike.