Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_downloadable.php
Go to the documentation of this file.
1 <?php
11  ->setTypeId(\Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE)
12  ->setId(1)
13  ->setAttributeSetId(4)
14  ->setWebsiteIds([1])
15  ->setName('Downloadable Product')
16  ->setSku('downloadable-product')
17  ->setPrice(10)
18  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
19  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
20  ->setLinksPurchasedSeparately(true)
21  ->setStockData(
22  [
23  'qty' => 100,
24  'is_in_stock' => 1,
25  'manage_stock' => 1,
26  ]
27  );
28 
33  ->get(\Magento\Downloadable\Api\Data\LinkInterfaceFactory::class);
34 $links = [];
36  'title' => 'Downloadable Product Link',
39  'link_url' => 'http://example.com/downloadable.txt',
40  'link_id' => 0,
41  'is_delete' => null,
42 ];
43 $link = $linkFactory->create(['data' => $linkData]);
44 $link->setId(null);
45 $link->setLinkType($linkData['type']);
46 $link->setStoreId($product->getStoreId());
47 $link->setWebsiteId($product->getStore()->getWebsiteId());
48 $link->setProductWebsiteIds($product->getWebsiteIds());
49 $link->setSortOrder(1);
50 $link->setPrice(0);
51 $link->setNumberOfDownloads(0);
53 $extension = $product->getExtensionAttributes();
54 $extension->setDownloadableProductLinks($links);
55 $product->setExtensionAttributes($extension);
56 
58  ->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$productRepository