Programmatically create a simple product in magento 2
Here, I found the solutions to create a product programmatically via custom php file i.e. test.php
.
<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$_product = $objectManager->create('Magento\Catalog\Model\Product');
$_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, //'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' => 100 //qty
)
);
$_product->save();
?>
First, in your constructor you'll want to include three classes for dependency injection: Magento\Catalog\Api\Data\ProductInterfaceFactory
, Magento\Catalog\Api\ProductRepositoryInterface
and Magento\CatalogInventory\Api\StockRegistryInterface
. The first is generated, so don't get too concerned if it shows up as not existing in your IDE.
public function __construct(\Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry)
{
$this->productFactory = $productFactory;
$this->productRepository = $productRepository;
$this->stockRegistry = $stockRegistry;
}
From there, where you want to create the product, you'll need to use the Factory to create it and set the data, and the repository to save it:
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
$product = $this->productFactory->create();
$product->setSku('SAMPLE-ITEM');
$product->setName('Sample Item');
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
$product->setVisibility(4);
$product->setPrice(1);
$product->setAttributeSetId(4); // Default attribute set for products
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
// If desired, you can set a tax class like so:
$product->setCustomAttribute('tax_class_id', $taxClassId);
$product = $this->productRepository->save($product); // This is important - the version provided and the version returned will be different objects
You'll likely then want to add some stock for it, which you can do like this:
$stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
$stockItem->setIsInStock($isInStock);
$stockItem->setQty($stockQty);
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
If you're running this in a script (including setup/upgrade scripts), then you're also going to need to emulate the area as this sort of thing requires sessions for some crazy reason.
To do that, pull in \Magento\Framework\App\State
through the constructor, and then utilize this code:
$this->state->emulateAreaCode(
'adminhtml',
function () {
/* all code here */
}
);
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();
$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');
// 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');
try{
$set_product->setWebsiteIds(array(1));
$set_product->setAttributeSetId(4);
$set_product->setTypeId('simple');
$set_product->setCreatedAt(strtotime('now'));
// time of product creation
$set_product->setName('Test Sample Products');
// add Name of Product
$set_product->setSku('add-sku-1');
// add sku hear
$set_product->setWeight(1.0000);
// add weight of product
$set_product->setStatus(1);
$category_id= array(4,5);
// add your catagory id
$set_product->setCategoryIds($category_id);
// Product Category
$set_product->setTaxClassId(0);
// type of tax class
// (0 - none, 1 - default, 2 - taxable, 4 - shipping)
$set_product->setVisibility(4);
// catalog and search visibility
$set_product->setManufacturer(28);
// manufacturer id
$set_product->setColor(24);
//print_r($_product);die;
$set_product->setNewsFromDate($today_date);
// product set as new from
$set_product->setNewsToDate($added_date);
// add image path hear
$set_product->setImage('/testimg/test.jpg');
// add small image path hear
$set_product->setSmallImage('/testimg/test.jpg');
// add Thumbnail image path hear
$set_product->setThumbnail('/testimg/test.jpg');
// product set as new to
$set_product->setCountryOfManufacture('AF');
// country of manufacture (2-letter country code)
$set_product->setPrice(100.99) ;
// price in form 100.99
$set_product->setCost(88.33);
// price in form 88.33
$set_product->setSpecialPrice(99.85);
// special price in form 99.85
$set_product->setSpecialFromDate('06/1/2016');
// special price from (MM-DD-YYYY)
$set_product->setSpecialToDate('06/30/2018');
// special price to (MM-DD-YYYY)
$set_product->setMsrpEnabled(1);
// enable MAP
$set_product->setMsrpDisplayActualPriceType(1);
// display actual price
// (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
$set_product->setMsrp(99.99);
// 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');
$set_product->setStockData(
array(
'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
)
);
$set_product->save();
// 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
Mage::log($exception->getMessage());
}
code reference :: http://www.onlinecode.org/create-a-simple-product-programmatically-in-magento-2/