Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Create a product by installer in magento2

     

    To create a product by installer you need to add file “InstallData.php” at path:

    app/code/Webkul/Hello/Setup/InstallData.php

    <?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,

    \Magento\Catalog\Model\Product\Type : is used to access type of product.
    $this->productModel->getDefaultAttributeSetId(): used to get get default attribute set id
    \Magento\Catalog\Model\Product\Visibility : used to get visibility of product
    \Magento\Catalog\Model\Product\Attribute\Source\Status: used to get status of product
    $this->storeManager->getDefaultStoreView()->getWebsiteId(): used to get website id.
    \Magento\Store\Model\Store: used to get store id
    
    
    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

    . . .

    Comment (1)

    Add Your Comment

  • Anil
    Hi Bulbul,
    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.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close