I have created a custom product. i.e

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <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"/>
            <attribute name="refundable" value="true"/>
            <attribute name="is_real_product" value="false"/>
            <attribute name="taxable" value="true"/>

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();

                    '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



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
                                'use_config_manage_stock' => 0,
                                'manage_stock' => 1,
                                'is_in_stock' => 1,
                                'qty' => 999999999

    // Adding Image to product
    $imagePath = "sample.jpg"; // path of the image
    $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);

    // Adding Custom option to product
    $options = array(
                        "sort_order"    => 1,
                        "title"         => "Custom Option 1",
                        "price_type"    => "fixed",
                        "price"         => "10",
                        "type"          => "field",
                        "is_require"   => 0
                        "sort_order"    => 2,
                        "title"         => "Custom Option 2",
                        "price_type"    => "fixed",
                        "price"         => "20",
                        "type"          => "field",
                        "is_require"   => 0
    foreach ($options as $arrayOption) {
        $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.