Magento 2 has the feature to create Product Programmatically from the backend but there is the time at which you want to create multiple products to test page performance, you need to create products in a certain format, you have some custom attribute or you have created your own attribute set or product type. It’s when you need to create products programmatically to ease the task and save time. Here, I’ve come up with the custom code to implement to create a simple product programmatically in Magento 2!

The below method allows creating simple product programmatically along with custom options. Moreover, you can add images to the media gallery of the product with the below method.

Create a Simple Product Programmatically in Magento 2:


getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');

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

try {
    $product->setName('Test Product');
    $product->setTypeId('simple');
    $product->setAttributeSetId(4);
    $product->setSku('test-SKU');
    $product->setWebsiteIds(array(1));
    $product->setVisibility(4);
    $product->setPrice(array(1));
    $product->setImage('/testimg/test.jpg');
    $product->setSmallImage('/testimg/test.jpg');
    $product->setThumbnail('/testimg/test.jpg');
    $product->setStockData(array(
            'use_config_manage_stock' => 0,
            'manage_stock' => 1,
            'min_sale_qty' => 1,
            'max_sale_qty' => 2,
            'is_in_stock' => 1,
            'qty' => 100
        )
    );

    $product->save();

    // Adding Custom option to product
    $options = array(
        array(
            "sort_order" => 1,
            "title" => "Custom Option 1",
            "price_type" => "fixed",
            "price" => "10",
            "type" => "field",
            "is_require" => 0
        ),
        array(
            "sort_order" => 2,
            "title" => "Custom Option 2",
            "price_type" => "fixed",
            "price" => "20",
            "type" => "field",
            "is_require" => 0
        )
    );
    foreach ($options as $arrayOption) {
        $product->setHasOptions(1);
        $product->getResource()->save($product);
        $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
            ->setProductId($product->getId())
            ->setStoreId($product->getStoreId())
            ->addData($arrayOption);
        $option->save();
        $product->addOption($option);
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

Note: There are some points you need to remember when using this code snippet.

1- If you want to create a configurable product then you have to assign associated product to current product otherwise it will create a simple product.

2- If you do not set weight, it will create a simple product.

3- If you want to create a downloadable product then you have to add download links to current product otherwise it will create virtual/simple product.