0

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
    $product->setIsDownloadable(1);

    $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',
                        )
                    );
    $product->setDownloadable($doanloadable_data);


    $product->save();

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

1 Answer 1

2

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_interface->setPrice(9);
    $link_interface->setNumberOFDownloads(10);
    $link_interface->setIsShareable(1);
    $link_interface->setLinkType('url');
    $link_interface->setLinkUrl('http://www.w3solver.com/download/qrcode.zip');
    $link_interface->setSampleType('url');
    $link_interface->setSampleUrl('http://w3solver.com');
    $link_interface->setIsUnlimited(0);
    $link_interface->setSortOrder(0);
    $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_interface->setSampleType('url');
    $sample_interface->setSampleUrl('http://www.w3solver.com/download/qrcode.zip');
    $sample_interface->setSortOrder(0);

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

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.