Magento 2 create product programmatically

Today we discussed how in Magento 2 create product programmatically. This tutorial includes the process of creating a simple product, set image to product and set quantity to product. So lets start with creating a simple product. We can do this in two ways- one is creating product with factory method and other is creating product with object manager.

Create Product with Factory Method

  protected $_product;  


  public function __construct(
     
        \Magento\Catalog\Model\ProductFactory $_product

    ) {


        $this->_productloader = $_productloader;
     
    }
    public function createProduct()
    {
       
        $_product = $this->_productloader->create();
        $_product->setName('First Test Product');
        $_product->setTypeId('simple');
        $_product->setAttributeSetId(4);
        $_product->setSku('test-SKU');
        $_product->setWebsiteIds(array(1));
        $_product->setVisibility(4);
        $_product->setPrice(400);
        $_product->setImage('/simpeproduct/test.jpg');
        $_product->setSmallImage('/simpeproduct/test.jpg');
        $_product->setThumbnail('/simpeproduct/test.jpg');
        $_product->setStockData(array(
            'use_config_manage_stock' => 0, //'Use config settings' checkbox
            'manage_stock' => 1, //manage stock
            'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
            'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
            'is_in_stock' => 1, //Stock Availability
            'qty' => 1000 //qty
            )
        );

        $_product->save();
        echo $_product->getId();
    }

Update Product with factory method

  protected $_product;  


  public function __construct(
     
        \Magento\Catalog\Model\ProductFactory $_product

    ) {


        $this->_productloader = $_productloader;
     
    }
    public function updateProduct()
    {
       
        $_product = $this->_productloader->create()->load(2047);
        $_product->setName('First Test Product Updated');
        $_product->save();
    }

Create Product with Object manager

 <?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $objectManager->create('Magento\Catalog\Model\Product');
    $_product->setName('First Test Product');
    $_product->setTypeId('simple');
    $_product->setAttributeSetId(4);
    $_product->setSku('test-SKU');
    $_product->setWebsiteIds(array(1));
    $_product->setVisibility(4);
    $_product->setPrice(400);
    $_product->setImage('/simpeproduct/test.jpg');
    $_product->setSmallImage('/simpeproduct/test.jpg');
    $_product->setThumbnail('/simpeproduct/test.jpg');
    $_product->setStockData(array(
            'use_config_manage_stock' => 0, //'Use config settings' checkbox
            'manage_stock' => 1, //manage stock
            'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
            'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
            'is_in_stock' => 1, //Stock Availability
            'qty' => 1000 //qty
            )
        );

$_product->save();
echo $_product->getId();

Update Product with Object manager

Now our custom product is created. Now we will update product. So doing with following example just.

            $_product = $objectManager->create('Magento\Catalog\Model\Product')->load(2047);
    $_product->setName('First Test Product Updated');
    $_product->save(); ?>

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply