How To Programmatically Create Downloadable Product In Magento 2

How To Programmatically Create Downloadable Product In Magento 2

Magento 2 downloadable product is any product that you can deliver in the form of a file.

There are six product types in Magento 2:

  1. Simple Product
  2. Configurable Product
  3. Grouped Product
  4. Virtual Product
  5. Bundle Product
  6. Downloadable Product

Out of these, the downloadable products, are such as an ebook, music, video, or text file, or a software application.

As a store owner, you can configure the products to be able to access based on the login status of the customer. Also, you can deliver the product to the customers directly by allowing them to download from your site or send them via an email.

The downloadable products can be uploaded to the server or linked to from another server. Also, you can allow the customers to download the product for the specific number of times. The delivery of a downloadable product can be made when the order is in either a Pending or Invoiced state.

Learn the easy way to programmatically create downloadable product in Magento 2 with the below code.

Method to programmatically create downloadable product in Magento 2:

That’s it.

Any doubts in the implementation of creating Magento 2 downloadable products? Please mention them in the Comments section below. I’d be happy to help.

I’d be grateful if you help me share the post with Magento folks via your social media profiles.

Thank you.

5
(based on 7 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

2 comments On How To Programmatically Create Downloadable Product In Magento 2

  • HI Sanjay, thanks for the example!

    Maybe can you provide an example where the downloadable is type FILE and not URL. I am struggling to get it working. I know it needs a JSON encoded block to describe the file as i have it working on M1 but M2 says it can’t find the file when saving the product although i know it’s there on the server.

    • Hello,
      Please refer the below code:
      $link_repository = $objectManager->create('Magento\Downloadable\Api\LinkRepositoryInterface');
      $link_interface = $objectManager->create('\Magento\Downloadable\Api\Data\LinkInterface');
      $link_interface->setTitle('product title');
      $link_interface->setPrice(20);
      $link_interface->setNumberOFDownloads(30);
      $link_interface->setIsShareable(1);
      $link_interface->setLinkType('url');
      $link_interface->setLinkUrl('Enter url here');
      $link_interface->setSampleType('url');
      $link_interface->setSampleUrl('Enter url here');
      $link_interface->setIsUnlimited(0);
      $link_interface->setSortOrder(0);
      $link_repository->save('new sku', $link_interface); // param1 is the sku of your product
      } catch (\Exception $e) {
      print_r($e->getMessage());
      }

      Thanks.

Leave a Reply

SUBSCRIBE
3.8
(based on 5 Reviews)