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;