how to magento2 create simple product Programmatically

how to magento2 create simple product Programmatically

How to create magento2 create simple product Programmatically and hot to use it.

In this we will show you how to upload simple product programmatically in magento2 with externale php file/script. Hear we include bootstrap.php file and pass product data for simple product upload programmatically.

This code will upload simple product in to your product list. Add this file in root of your magento2

code for magento2 create simple product

use \Magento\Framework\App\Bootstrap;

// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('\Magento\Framework\App\State');

// get date 
$today_date = date("m/d/Y");
$added_date = date('m/d/Y',strtotime("+17 day"));

$set_product = $object_Manager->create('\Magento\Catalog\Model\Product');

	// time of product creation
	$set_product->setName('Test Sample Products'); 
	// add Name of Product
	// add sku hear
	// add weight of product
	$category_id= array(4,5);
	// add your catagory id
	// Product Category
	// type of tax class 
	// (0 - none, 1 - default, 2 - taxable, 4 - shipping)
	// catalog and search visibility
	// manufacturer id
	// product set as new from
	// add image path hear
	// add small image path hear
	// add Thumbnail image path hear
	// product set as new to
	// country of manufacture (2-letter country code)
	$set_product->setPrice(100.99) ;
	// price in form 100.99
	// price in form 88.33
	// special price in form 99.85
	// special price from (MM-DD-YYYY)
	// special price to (MM-DD-YYYY)
	// enable MAP
	// display actual price 
	// (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
	// Manufacturer's Suggested Retail Price
	$set_product->setMetaTitle('test meta title 2');
	$set_product->setMetaKeyword('test meta keyword 2');
	$set_product->setMetaDescription('test meta description 2');
	$set_product->setDescription('This is a long description');
	$set_product->setShortDescription('This is a short description');
		'use_config_manage_stock' => 0, 
		// checkbox for 'Use config settings' 
		'manage_stock' => 1, // manage stock
		'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed 
		'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
		'is_in_stock' => 1, // Stock Availability of product
		'qty' => 100 // qty of product

	// get id of product
	$get_product_id = $set_product->getId();
	echo "Upload simple product id :: ".$get_product_id."\n";
catch(Exception $exception)
	// errro in exception/code

Hope this code and post will helped you for implement magento2 create simple product Programmatically. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

If do you have any suggestion please make a comments.

Leave a Comment

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

  +  35  =  44

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US