Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
downloadable_product_with_files_and_sample_url.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
15  ->setTypeId(\Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE)
16  ->setId(1)
17  ->setAttributeSetId(4)
18  ->setWebsiteIds([1])
19  ->setName('Downloadable Product')
20  ->setSku('downloadable-product')
21  ->setPrice(10)
22  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
23  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
24  ->setLinksPurchasedSeparately(true)
25  ->setStockData(
26  [
27  'qty' => 100,
28  'is_in_stock' => 1,
29  'manage_stock' => 1,
30  ]
31  )->setLinksTitle(
32  'Downloadable Product Link Title'
33  );
34 
38 $linkFactory = $objectManager->get(\Magento\Downloadable\Api\Data\LinkInterfaceFactory::class);
39 $links = [];
41  'title' => 'Downloadable Product Link',
44  'link_url' => 'http://example.com/downloadable.txt',
45  'link_id' => 0,
46  'is_delete' => null,
47 ];
48 $link = $linkFactory->create(['data' => $linkData]);
49 $link->setId(null);
50 $link->setLinkType($linkData['type']);
51 $link->setStoreId($product->getStoreId());
52 $link->setWebsiteId($product->getStore()->getWebsiteId());
53 $link->setProductWebsiteIds($product->getWebsiteIds());
54 $link->setSortOrder(1);
55 $link->setPrice(0);
56 $link->setNumberOfDownloads(0);
58 
59 $linkData = [
60  'title' => 'Downloadable Product Link',
61  'sample' => [
63  'url' => 'http://sampleUrl.com',
64  ],
67  'link_url' => 'http://example.com/downloadable.txt',
68  'is_delete' => null,
69  'number_of_downloads' => 0,
70  'price' => 0,
71 ];
75 $sampleContent = $objectManager->create(\Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class)->create();
76 $sampleContent->setFileData(
77  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
78 );
79 $sampleContent->setName('jellyfish_1_3.jpg');
80 $sampleLink = $linkFactory->create(['data' => $linkData]);
81 $sampleLink->setId(null);
82 $sampleLink->setSampleType($linkData['sample']['type']);
83 $sampleLink->setSampleFileContent($sampleContent);
84 $sampleLink->setSampleUrl($linkData['sample']['url']);
85 $sampleLink->setLinkType($linkData['type']);
86 $sampleLink->setStoreId($product->getStoreId());
87 $sampleLink->setWebsiteId($product->getStore()->getWebsiteId());
88 $sampleLink->setProductWebsiteIds($product->getWebsiteIds());
89 $sampleLink->setSortOrder(2);
93 $content = $objectManager->create(\Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class)->create();
94 $content->setFileData(
95  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
96 );
97 $content->setName('jellyfish_2_4.jpg');
98 //$content->setName('');
99 $sampleLink->setLinkFileContent($content);
100 $links[] = $sampleLink;
101 
103  'sample' => [
104  [
105  'is_delete' => 0,
106  'sample_id' => 0,
107  'title' => 'Downloadable Product Sample Title',
109  'file' => json_encode(
110  [
111  [
112  'file' => '/f/u/jellyfish_1_4.jpg',
113  'name' => 'jellyfish_1_4.jpg',
114  'size' => 1024,
115  'status' => 0,
116  ],
117  ]
118  ),
119  'sample_url' => null,
120  'sort_order' => '0',
121  ],
122  ],
123 ];
124 
125 $extension = $product->getExtensionAttributes();
126 
127 $samples = [];
128 $sampleFactory = $objectManager->create(\Magento\Downloadable\Api\Data\SampleInterfaceFactory::class);
129 foreach ($downloadableData['sample'] as $sampleData) {
130  if (!$sampleData || (isset($sampleData['is_delete']) && (bool)$sampleData['is_delete'])) {
131  continue;
132  } else {
133  unset($sampleData['sample_id']);
137  $sample = $sampleFactory->create(['data' => $sampleData]);
138  $sample->setId(null);
139  $sample->setStoreId($product->getStoreId());
140  $sample->setSampleType($sampleData['type']);
141  $sample->setSampleUrl($sampleData['sample_url']);
145  $content = $objectManager->create(
146  \Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class
147  )->create();
148  $content->setFileData(
149  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
150  );
151  $content->setName('jellyfish_1_4.jpg');
152  $sample->setSampleFileContent($content);
153  $sample->setSortOrder($sampleData['sort_order']);
154  $samples[] = $sample;
155  }
156 }
157 
158 $extension = $product->getExtensionAttributes();
159 $extension->setDownloadableProductSamples($samples);
160 $extension->setDownloadableProductLinks($links);
161 $product->setExtensionAttributes($extension);
162 
164 $productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
165 $productRepository->save($product)->getData();
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60