How to create new object Product ?
Example:
$product = new Product();
$product->sku = 'fdfds';
...
$product->save();
How to create new object Product ?
Example:
$product = new Product();
$product->sku = 'fdfds';
...
$product->save();
Note: This is a quick dirty code, use dependency instead.
Here is example how to create product model object :
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('sku'); // Set your sku here
$product->save();
?>
Another example : You can use it like create a file on root example.php and save the below code:
<?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->save();
?>
It is recommended to use dependency injection rather than directly using object Manager. Example: In your controller file you can use following code :
protected $_product;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Product $productFactory,
array $data = []
) {
$this->_product = $productFactory;
parent::__construct($context, $data);
}
public function execute()
{
$this->_product->setSku('test-simple');
$this->_product->setName('test name simple');
$this->_product->setAttributeSetId(4);
$this->_product->setSize_general(193);
$this->_product->setStatus(1);
$this->_product->setTypeId('simple');
$this->_product->setPrice(10);
$this->_product->setWebsiteIds(array(1));
$this->_product->setCategoryIds(array(31));
$this->_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
)
);
$this->_product->save();
}
catalog_product_save_after
Commented
May 31, 2017 at 7:44
This is a very old question, but the selected answer is completly wrong.
The answer from Manashvi Birla is kind of good but it has 2 problems
If you wish to use factory on product model class, you will have to do the following
/**
* SaveProduct constructor.
* @param \Magento\Catalog\Model\ProductFactory $product
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
public function __construct(
\Magento\Catalog\Model\ProductFactory $product,
\Magento\Catalog\Model\ProductRepository $productRepository
) {
$this->product = $product->create();
$this->productRepository = $productRepository;
}
<?php
class SaveProduct
{
private $product;
private $productRepository;
/**
* SaveProduct constructor.
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
public function __construct(
\Magento\Catalog\Model\Product $product,
\Magento\Catalog\Model\ProductRepository $productRepository
) {
$this->product = $product;
$this->productRepository = $productRepository;
}
/**
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
public function execute(): void
{
// example product defination
$this->product->setSku('test-simple');
$this->product->setName('test name simple');
$this->product->setAttributeSetId(4);
$this->product->setSize_general(193);
$this->product->setStatus(1);
$this->product->setTypeId('simple');
$this->product->setPrice(10);
$this->product->setWebsiteIds([1]);
$this->product->setCategoryIds([31]);
// repository saves product
$this->productRepository->save($this->product);
}
}