I know how to add a simple product but cannot create a downloadable product programmatically. I am using following code for adding downloable product information:-

$product->setProductHasWeight(0); // weight of product

    $doanloadable_data = array(
                    'link' =>array('title' => 'first downloable product',
                       'price' => 9,
                       'number_of_downloads' => 10,
                       'is_shareable' => 1,
                       'type' => 'url',
                       'link_url' => 'http://w3solver.com',
                       'sample' => array('url'=>'http://w3solver.com',
                                        'type' => 'url'),
                       'is_unlimited' => 0,
                    'sample' => array('title' => 'sample',
                        'sample_url' => 'http://w3solver.com',
                        'type' => 'url',


Anyone here can please help me to find out what is the issue over here or what I can do make it work.


Finally I found it and explained it on this post(In case it might help someone in future)

To create a downloadable product we need to set productHasWeight as zero and typeId as downloadable, once our product is created we have to provide information of link and sample as shown in below code:-

    $link_repository = $objectManager->create('Magento\Downloadable\Api\LinkRepositoryInterface');
    $link_interface = $objectManager->create('\Magento\Downloadable\Api\Data\LinkInterface');
    $link_interface->setTitle('first downloable product');
    $link_repository->save('sku', $link_interface);

    $sample_repository = $objectManager->create('Magento\Downloadable\Api\SampleRepositoryInterface');
    $sample_interface = $objectManager->create('\Magento\Downloadable\Api\Data\SampleInterface');
    $sample_interface->setTitle('first downloable product');

    $sample_repository->save('sku', $sample_interface); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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