How to Create Simple Product Programmatically in Magento 2

Hello Magento Folks,

In this tutorial blog, I will illustrate How to Create Simple Product Programmatically in Magento 2. When you work with Magento then you might be familiar with all the product types. If you are not familiar then check out my previous article where I have explained all the types of products in Magento 2

Basically while working in Magento 2 you will come across the types of products. And if you are in requirement of a large number of products then it will take much time if you go to create each and every product via the admin panel. Therefore for this kind of requirement, Magento allows creating products programmatically. To perform this task then follow the below instructed steps to Create Simple Product Programmatically in Magento 2.

Mainly we can create 6 types of products Programmatically in Magento 2:

  • Simple Product
  • Configurable Product
  • Grouped Product
  • Virtual Product
  • Bundle Product
  • Downloadable Product

Steps to Create Simple Product Programmatically in Magento 2:

Step 1 : Firstly, create a simple_product.php file in the Magento root directory and add the below given code.

<?php

use Magento\Framework\AppInterface;

try {

    require_once __DIR__ . '/app/bootstrap.php';

} catch (\Exception $e) {

    echo 'Autoload error: ' . $e->getMessage();

    exit(1);

}

try{

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();


$appState = $objectManager->get('Magento\Framework\App\State');

$appState->setAreaCode('frontend');


$product = $objectManager->create('Magento\Catalog\Model\Product');

$sku = 'your_sku';  // set your sku

$product->setSku($sku);

$product->setName('Simple Product Name'); // set your Product Name of Product

$product->setAttributeSetId(4); // set attribute id

$product->setStatus(1); // status enabled/disabled 1/0

$product->setWeight(1); // set weight of product

$product->setVisibility(4); // visibility of product (Not Visible Individually (1) / Catalog (2)/ Search (3)/ Catalog, Search(4))

$product->setWebsiteIds(array(1));

$product->setTaxClassId(0); // Tax class ID

$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)

$product->setPrice(100); // set price of product

$product->setStockData(

      array(

      'use_config_manage_stock' => 0,

      'manage_stock' => 1,

      'is_in_stock' => 1,

      'qty' => 100
      )
    );

$product->save();

$categoryIds = array('2','3'); // assign your product to category using Category Id

    $category = $objectManager->get('Magento\Catalog\Api\CategoryLinkManagementInterface');

$category->assignProductToCategories($sku, $categoryIds);

echo "$sku Product Created Successfully ";

}

catch(\Exception $e){

print_r($e->getMessage());

}

Step 2: After the above step you will need to run the below-given URL to create a simple product.

https://yourdomain.com/simple_product.php

Wrap Up:

Hopefully, with the above code all are able to Create Simple Product Programmatically in your Magento 2. As always, don’t forget to clear cache and reindex if you are unable to view the output on the frontend of your store. In case you require some help during the implementation of the above illustration then let me know in the comment section below I will help you there. 

Share the article with your friends and family

Happy Reading!

Previous Article

The WordPress Website: A Fruitful Investment Today

Next Article

How to Create Configurable Product Programmatically in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨