How to Develop Downloadable Product Programmatically in Magento 2?

Today's tutorial brings you a guide on how to create downloadable product programmatically in Magento 2. Some great examples of the downloadable product can be eBook, music, video, software application etc. Admin can also configure it in such a way that customer is required to log in, to receive the link.

While developing some functionality related to the downloadable product, maybe we need to create a variety of downloadable products. It is always difficult to create downloadable products programmatically, let me show you an easy way and build a script for the same.

Steps to Create a Downloadable Product in Magento 2

Here are the steps to create a downloadable product in Magento 2 programmatically.

Step 1. Create a downloadable.php file at your Magento’s root folder and add this below code (It’s Working Code).

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $objectManager->create('Magento\Catalog\Model\Product');

$product->setName('Downloadable Product'); // Set Product Name
$product->setTypeId('downloadable'); // Set Product Type Id
$product->setAttributeSetId(14); // Set Attribute Set ID
$product->setSku('downloadable-product'); // Set SKU
$product->setStatus(1); // Set Status
$product->setTaxClassId(2); // Set Tax Class Id
$product->setWebsiteIds([1]); // Set Website Ids
$product->setVisibility(4);
$product->setCategoryIds([9, 10]); // Assign Category Ids
$product->setPrice(100); // Product Price
$product->setPriceType(0);
$product->setPriceView(0);
$product->setImage('/downloadable/test.jpg'); // Image Path
$product->setSmallImage('/downloadable/test.jpg'); // Small Image Path
$product->setThumbnail('/downloadable/test.jpg'); // Thumbnail Image Path

try {
    $product->save();
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $baseUrl = $storeManager->getStore()->getBaseUrl();
    $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($baseUrl . 'demo.jpg');
    $link_interface->setSampleType('url');
    $link_interface->setSampleUrl($baseUrl . 'demo.jpg');
    $link_interface->setIsUnlimited(0);
    $link_interface->setSortOrder(0);
    $link_repository->save($product->getSku(), $link_interface); // param1 is the sku of your product

    $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($baseUrl . 'demo.jpg');
    $sample_interface->setSortOrder(0);
    $sample_repository->save($product->getSku(), $sample_interface);
    
    if ($product->getId()) {
        echo "Product Created Successfully";
    }
} catch (Exception $ex) {
    echo '<pre>';
    print_r($ex->getMessage());
    exit;
}

Here yo go, It is ready!

In addition, static details can be set based on your requirements. If you are planning to create multiple products programmatically with the script, you can use this script and customize code for multiple products.

Now, execute this file at frontend side : {baseUrl}/downloadable.php

Hope this tutorial helped you create the downloadable programmatically. If we missed out anything, feel free to reach us out!

Our experts will be happy to help.