Magento, Magento 2

How to Create Downloadable Product Programmatically in Magento 2

How to Create Downloadable Product Programmatically in Magento 2

In this tutorial, I will explain to how to create downloadable product programmatically in Magento 2. Downloadable product will be like eBook, music, video, software application etc. It can be configure to require login customer to receive the link or can be sent by email.

So, Whenever we develop some functionality related to downloadable product at that time, maybe we need to create large number of downloadable product. But, if we are going to create from admin panel then, it will take a time to create large amount of downloadable products. But, it’s sometimes confusing stuff to create downloadable product programmatically.

So, Let’s make easy way and create script for that.

You may also like this :

List of article to create product programmatically with different types :

First of all, Create a downloadable.php file at your Magento’s root folder and add this below code (It’s Working Code).


<?php
/**
 * Created By : Rohan Hapani
 */
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;
}

That’s it !!

In addition, You can set static details based on your requirements. If you want to create multiple products programmatically by the script at that time, you can use this script and customize code for multiple products create.

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

I hope this blog will helpful for easily understand how to create downloadable product programmatically in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing 🙂

 

Tagged ,