Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_downloadable_with_files.php
Go to the documentation of this file.
1 <?php
7 \Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
12 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
13 $sampleFactory = $objectManager->create(\Magento\Downloadable\Api\Data\SampleInterfaceFactory::class);
14 $linkFactory = $objectManager->create(\Magento\Downloadable\Api\Data\LinkInterfaceFactory::class);
15 
17  'sample' => [
18  [
19  'is_delete' => 0,
20  'sample_id' => 0,
21  'title' => 'Downloadable Product Sample Title',
23  'file' => json_encode(
24  [
25  [
26  'file' => '/f/u/jellyfish_1_4.jpg',
27  'name' => 'jellyfish_1_4.jpg',
28  'size' => 1024,
29  'status' => 0,
30  ],
31  ]
32  ),
33  'sample_url' => null,
34  'sort_order' => '0',
35  ],
36  ],
37 ];
38 $product->setTypeId(
39  \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
40 )->setId(
41  1
42 )->setAttributeSetId(
43  4
44 )->setWebsiteIds(
45  [1]
46 )->setName(
47  'Downloadable Product'
48 )->setSku(
49  'downloadable-product'
50 )->setPrice(
51  10
52 )->setVisibility(
53  \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
54 )->setStatus(
55  \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
56 );
57 
58 $extension = $product->getExtensionAttributes();
59 $links = [];
61  'product_id' => 1,
62  'sort_order' => '0',
63  'title' => 'Downloadable Product Link',
64  'sample' => [
66  'url' => null,
67  ],
70  'link_url' => null,
71  'is_delete' => 0,
72  'number_of_downloads' => 15,
73  'price' => 15.00,
74 ];
75 $link = $linkFactory->create(['data' => $linkData]);
76 $link->setId(null);
77 $link->setSampleType($linkData['sample']['type']);
78 
82 $content = $objectManager->create(\Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class)->create();
83 $content->setFileData(
84  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
85 );
86 $content->setName('jellyfish_2_4.jpg');
87 //$content->setName('');
88 $link->setLinkFileContent($content);
89 
93 $sampleContent = $objectManager->create(\Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class)->create();
94 $sampleContent->setFileData(
95  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
96 );
97 $sampleContent->setName('jellyfish_1_3.jpg');
98 $link->setSampleFileContent($sampleContent);
99 $link->setSampleUrl($linkData['sample']['url']);
100 $link->setLinkType($linkData['type']);
101 $link->setStoreId($product->getStoreId());
102 $link->setWebsiteId($product->getStore()->getWebsiteId());
103 $link->setProductWebsiteIds($product->getWebsiteIds());
104 if (!$link->getSortOrder()) {
105  $link->setSortOrder(1);
106 }
107 if (null === $link->getPrice()) {
108  $link->setPrice(0);
109 }
110 if ($link->getIsUnlimited()) {
111  $link->setNumberOfDownloads(0);
112 }
114 
115 $extension->setDownloadableProductLinks($links);
116 
117 if (isset($downloadableData['sample']) && is_array($downloadableData['sample'])) {
118  $samples = [];
119  foreach ($downloadableData['sample'] as $sampleData) {
120  if (!$sampleData || (isset($sampleData['is_delete']) && (bool)$sampleData['is_delete'])) {
121  continue;
122  } else {
123  unset($sampleData['sample_id']);
127  $sample = $sampleFactory->create(['data' => $sampleData]);
128  $sample->setId(null);
129  $sample->setStoreId($product->getStoreId());
130  $sample->setSampleType($sampleData['type']);
131  $sample->setSampleUrl($sampleData['sample_url']);
135  $content = $objectManager->create(
136  \Magento\Downloadable\Api\Data\File\ContentInterfaceFactory::class
137  )->create();
138  $content->setFileData(
139  base64_encode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'test_image.jpg'))
140  );
141  $content->setName('jellyfish_1_4.jpg');
142  $sample->setSampleFileContent($content);
143  $sample->setSortOrder($sampleData['sort_order']);
144  $samples[] = $sample;
145  }
146  }
147  $extension->setDownloadableProductSamples($samples);
148 }
149 $product->setExtensionAttributes($extension);
150 
151 if ($product->getLinksPurchasedSeparately()) {
152  $product->setTypeHasRequiredOptions(true)->setRequiredOptions(true);
153 } else {
154  $product->setTypeHasRequiredOptions(false)->setRequiredOptions(false);
155 }
156 $product->save();
157 
158 $stockRegistry = $objectManager->get(\Magento\CatalogInventory\Api\StockRegistryInterface::class);
159 $stockItem = $stockRegistry->getStockItem($product->getId());
160 $stockItem->setUseConfigManageStock(true);
161 $stockItem->setQty(100);
162 $stockItem->setIsInStock(true);
163 $stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60