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:
- Simple Product
- Configurable Product
- Grouped Product
- Virtual Product
- Bundle Product
- 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:
<?php use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager $product = $objectManager->create('\Magento\Catalog\Model\Product'); try { $product->setSku('sku'); // Set your sku here $product->setName('Product Name'); // Name of Product $product->setAttributeSetId(4); // Attribute set id $product->setStatus(1); // Status on product enabled/ disabled 1/0 $product->setWeight(10); // weight of product $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually) $product->setTaxClassId(0); // Tax class id $product->setTypeId('downloadable'); // type of product (simple/virtual/downloadable/configurable) $product->setPrice(500); // price of product $product->setLinkType('url'); // Set Link Type $product->setLinkUrl('path of url'); // Set Url Link $product->setSampleType('url'); // Set simple Link Type $product->setSampleUrl('url'); // Set Simple Url Link $product->setStockData( array( 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 40 ) ); // Adding Image to product $mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media'); $imagePath = "image.jpg"; // path of the image $imageFilename = basename($imagePath); $image_type = substr(strrchr($imageFilename, "."), 1); //find the image extension $filename = md5($imagePath . strtotime('now')) . '.' . $image_type; //give a new name, you can modify as per your requirement if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true); else chmod($mediaDir, 0777); $filepath = $mediaDir . '/catalog/product/' . $filename; //path for temp storage folder: pub/media file_put_contents($filepath, file_get_contents(trim($imagePath))); //store the image from external url to the temp storage folder $imgUrl = $filepath; $product->addImageToMediaGallery($imgUrl, ['image', 'small_image', 'thumbnail'], false, false); $product->save(); // Adding Custom option to product $options = array( array( "sort_order" => 1, "number_of_downloads" => 10, "is_shareable" => 1, "link_url" => "http://example.com", "link_type" => "url", "sample_url" => "http://example.com", "sample_type" => "url" ) ); foreach ($options as $arrayOption) { $product->setHasOptions(1); $product->getResource()->save($product); $option = $objectManager->create('\Magento\Catalog\Model\Product\Option') ->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($arrayOption); $option->save(); $product->addOption($option); } } catch (\Exception $e) { print_r($e->getMessage()); } ?>
That’s it.
I’d be grateful if you help me share the post with Magento folks via your social media profiles.
Thank you.