Back to Top

Magento2 – Programmatically Create Product

Updated 21 November 2016

Create Product Programatically

Here we will see how to create product in magento2 with given data.
Use following code to programmatically create product in magento2.

		$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
		$product = $objectManager->create('\Magento\Catalog\Model\Product');
		$product->setSku('sku'); // Set your sku here
		$product->setName('Sample Product'); // Name of Product
		$product->setAttributeSetId(4); // Attribute set id
		$product->setStatus(1); // Status on product enabled/ disabled 1/0
		$product->setWeight(10); // weight of product
		$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
		$product->setTaxClassId(0); // Tax class id
		$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
		$product->setPrice(100); // price of product
		$product->setStockData(
								array(
									'use_config_manage_stock' => 0,
									'manage_stock' => 1,
									'is_in_stock' => 1,
									'qty' => 999999999
								)
							);
		$product->save();

		// Adding Image to product
		$imagePath = "sample.jpg"; // path of the image
		$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
		$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);
		}

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

1- If you want to create configurable product then you have to assign associated product to current product otherwise it will create simple product. See here how to add associated products.

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

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

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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


3 comments

  • Amitkumar Magento Developer
    • Miroslav Kodet
  • AlaaGh
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home