Skip to content

Commit da565df

Browse files
author
Leonid Poluyanov
committed
MAGETWO-50649: Downloadable product has no links and samples in sample data
1 parent dcac1f6 commit da565df

File tree

1 file changed

+118
-1
lines changed
  • app/code/Magento/DownloadableSampleData/Model

1 file changed

+118
-1
lines changed

app/code/Magento/DownloadableSampleData/Model/Product.php

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\DownloadableSampleData\Model;
77

88
use Magento\Framework\Setup\SampleData\Context as SampleDataContext;
9+
use Magento\Downloadable\Api\Data\SampleInterfaceFactory as SampleFactory;
10+
use Magento\Downloadable\Api\Data\LinkInterfaceFactory as LinkFactory;
11+
use \Magento\Framework\App\ObjectManager;
912

1013
/**
1114
* Setup downloadable product
@@ -27,6 +30,16 @@ class Product extends \Magento\CatalogSampleData\Model\Product
2730
*/
2831
protected $downloadableData = [];
2932

33+
/**
34+
* @var SampleFactory
35+
*/
36+
protected $sampleFactory;
37+
38+
/**
39+
* @var LinkFactory
40+
*/
41+
protected $linkFactory;
42+
3043
/**
3144
* Product constructor.
3245
* @param SampleDataContext $sampleDataContext
@@ -44,7 +57,9 @@ public function __construct(
4457
\Magento\DownloadableSampleData\Model\Product\Converter $converter,
4558
\Magento\CatalogSampleData\Model\Product\Gallery $gallery,
4659
\Magento\Store\Model\StoreManagerInterface $storeManager,
47-
\Magento\Eav\Model\Config $eavConfig
60+
\Magento\Eav\Model\Config $eavConfig,
61+
SampleFactory $sampleFactory,
62+
LinkFactory $linkFactory
4863
) {
4964
parent::__construct(
5065
$sampleDataContext,
@@ -55,6 +70,8 @@ public function __construct(
5570
$storeManager,
5671
$eavConfig
5772
);
73+
$this->linkFactory = $linkFactory;
74+
$this->sampleFactory = $sampleFactory;
5875
}
5976

6077
/**
@@ -96,9 +113,109 @@ public function install(array $productFixtures, array $galleryFixtures, array $d
96113
protected function prepareProduct($product, $data)
97114
{
98115
if (isset($this->downloadableData[$data['sku']])) {
116+
$extension = $product->getExtensionAttributes();
117+
$links = [];
118+
foreach ($this->downloadableData[$data['sku']]['link'] as $linkData) {
119+
$link = $this->getLinkFactory()->create(['data' => $linkData]);
120+
if (isset($linkData['type'])) {
121+
$link->setLinkType($linkData['type']);
122+
}
123+
if (isset($linkData['file'])) {
124+
$link->setFile($linkData['file']);
125+
}
126+
if (isset($linkData['file_content'])) {
127+
$link->setLinkFileContent($linkData['file_content']);
128+
}
129+
$link->setId(null);
130+
if (isset($linkData['sample']['type'])) {
131+
$link->setSampleType($linkData['sample']['type']);
132+
}
133+
if (isset($linkData['sample']['file'])) {
134+
$link->setSampleFileData($linkData['sample']['file']);
135+
}
136+
if (isset($linkData['sample']['url'])) {
137+
$link->setSampleUrl($linkData['sample']['url']);
138+
}
139+
if (isset($linkData['sample']['file_content'])) {
140+
$link->setSampleFileContent($linkData['file_content']);
141+
}
142+
$link->setStoreId($product->getStoreId());
143+
$link->setWebsiteId($product->getStore()->getWebsiteId());
144+
$link->setProductWebsiteIds($product->getWebsiteIds());
145+
if (!$link->getSortOrder()) {
146+
$link->setSortOrder(1);
147+
}
148+
if (null === $link->getPrice()) {
149+
$link->setPrice(0);
150+
}
151+
if ($link->getIsUnlimited()) {
152+
$link->setNumberOfDownloads(0);
153+
}
154+
$links[] = $link;
155+
}
156+
$extension->setDownloadableProductLinks($links);
157+
158+
$samples = [];
159+
foreach ($this->downloadableData[$data['sku']]['sample'] as $sampleData) {
160+
$sample = $this->getSampleFactory()->create(['data' => $sampleData]);
161+
$sample->setId(null);
162+
$sample->setStoreId($product->getStoreId());
163+
if (isset($sampleData['type'])) {
164+
$sample->setSampleType($sampleData['type']);
165+
}
166+
if (isset($sampleData['file'])) {
167+
$sample->setFile($sampleData['file']);
168+
}
169+
if (isset($sampleData['sample_url'])) {
170+
$sample->setSampleUrl($sampleData['sample_url']);
171+
}
172+
if (!$sample->getSortOrder()) {
173+
$sample->setSortOrder(1);
174+
}
175+
$samples[] = $sample;
176+
}
177+
$extension->setDownloadableProductSamples($samples);
178+
99179
$product->setDownloadableData($this->downloadableData[$data['sku']]);
180+
$product->setExtensionAttributes($extension);
100181
}
101182
$this->setVirtualStockData($product);
102183
return $this;
103184
}
185+
186+
/**
187+
* Get link interface factory
188+
*
189+
* @deprecated
190+
* @return \Magento\Downloadable\Api\Data\LinkInterfaceFactory
191+
*/
192+
private function getLinkFactory()
193+
{
194+
195+
if (!($this->linkFactory)) {
196+
return ObjectManager::getInstance()->get(
197+
'\Magento\Downloadable\Api\Data\LinkInterfaceFactory'
198+
);
199+
} else {
200+
return $this->linkFactory;
201+
}
202+
}
203+
204+
/**
205+
* Get sample interface factory
206+
*
207+
* @deprecated
208+
* @return \Magento\Downloadable\Api\Data\SampleInterfaceFactory
209+
*/
210+
private function getSampleFactory()
211+
{
212+
213+
if (!($this->sampleFactory)) {
214+
return ObjectManager::getInstance()->get(
215+
'\Magento\Downloadable\Api\Data\SampleInterfaceFactory'
216+
);
217+
} else {
218+
return $this->sampleFactory;
219+
}
220+
}
104221
}

0 commit comments

Comments
 (0)