How to create downloadable product in Magento 2 programmatically ?

Welcome to Kishan Savaliya's Magento 2 Blog.In this blog post, I will explain how to create a downloadable product in Magento 2 programmatically. We can create downloadable product using backend too but in some case if you need to create downloadable products again and again then if you will create product from backend then it will take more time and instead of that you can do that easily with simple root script file, it will save your time and that is quite easy to do.

So let's start.. Move into your Magento 2 root directory and create below file there..

create_downloadableproduct.php

Content for this file is..

<?php
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$objectManager->get(Magento\Framework\App\State::class)->setAreaCode('frontend');
$product = $objectManager->create(Magento\Catalog\Model\Product::class);

try {
    $product->setTypeId(\Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE);
    $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
    $product->setAttributeSetId(14); //Set your attribute set ID, I used 14 because I have default Luma theme and I want to use Downloadable attribute set and it's attribute ID is 14, find your ID and set here.
    $product->setName('Downloadable Product name');
    $product->setSku('downloadable-product-sku');
    $product->setUrlKey('downloadable-product-url-key');
    $product->setPrice(100);
    $product->setWeight(3);
    $product->setStockData(
        [
            'use_config_manage_stock' => 0,
            'manage_stock' => 1,
            'is_in_stock' => 1,
        ]
    );
    $product->setDescription('<p>Downloadable Product Description</p>');
    $product->setShortDescription('<p>Downloadable Product Short Description</p>');
    $product->setWebsiteIds([1]); //Find your website ID and set that ID instead of 1 in array
    $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); //To display downloadable product in frontend you can set visibility to catalog, search

    $product->setPriceType(0);
    $product->setPriceView(0);

    $product->save();

    $storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
    $baseUrl = $storeManager->getStore()->getBaseUrl();
    $linkRepository = $objectManager->create(\Magento\Downloadable\Api\LinkRepositoryInterface::class);
    $linkInterface = $objectManager->create(\Magento\Downloadable\Api\Data\LinkInterface::class);
    $linkInterface->setTitle('Downloadable product');
    $linkInterface->setPrice(50);
    $linkInterface->setNumberOFDownloads(10);
    $linkInterface->setIsShareable(1);
    $linkInterface->setLinkType('url');
    $linkInterface->setLinkUrl($baseUrl . 'image.jpg');
    $linkInterface->setSampleType('url');
    $linkInterface->setSampleUrl($baseUrl . 'image.jpg');
    $linkInterface->setIsUnlimited(0);
    $linkInterface->setSortOrder(0);
    $linkRepository->save($product->getSku(), $linkInterface); // param1 is the sku of your product
    $sampleRepository = $objectManager->create(Magento\Downloadable\Api\SampleRepositoryInterface::class);
    $sampleInterface = $objectManager->create(\Magento\Downloadable\Api\Data\SampleInterface::class);
    $sampleInterface->setTitle('Downloadable product');
    $sampleInterface->setSampleType('url');
    $sampleInterface->setSampleUrl($baseUrl . 'image.jpg');
    $sampleInterface->setSortOrder(1);
    $sampleRepository->save($product->getSku(), $sampleInterface);
    
    if($product->getId()){
        echo "Downloadable Product created successfully.";
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

You can change above content based on your requiremrent. You can simply run below URL in browser it'll create downloadable product in your Magento.

{{magento_base_url}}/create_downloadableproduct.php

That's it.


Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.

If you enjoyed this blog post, share it with friends!