I have created a custom product. i.e
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
<type name="Dome" label="Event Dome Product" modelInstance="Mymodule\Example\Model\Product\Type\EventDome"
composite="false" isQty="true" canUseQtyDecimals="false" sortOrder="83">
<priceModel instance="Mymodule\Example\Model\Product\Type\Price"/>
<customAttributes>
<attribute name="refundable" value="true"/>
<attribute name="is_real_product" value="false"/>
<attribute name="taxable" value="true"/>
</customAttributes>
</type>
</config>
I now want to programmatically create products. The code below is employed on an admin controller page. It works, however, it does not create a fresh product but overrides the previously created product. i.e if I create a product and then try and use the same form to create another one it deletes the previous one.
Also, I am unable to set a price.
$_product = $this->productFactory->create();
$_product->setName($array['ProductName']);
$_product->setTypeId('Dome');
$_product->setAttributeSetId(4);
$_product->setSku('test-SKU');
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setPrice(array(350));
$_product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$_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 = $this->productRepository->save($_product);
$id = $product->getId();
return $id;
2 Answers 2
Sorry Everyone. I made a silly mistake. The SKU needs to be unique each time
i,e
$_product->setSku('test-SKU');
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);
}