6
6
namespace Magento \DownloadableSampleData \Model ;
7
7
8
8
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 ;
9
12
10
13
/**
11
14
* Setup downloadable product
@@ -27,6 +30,16 @@ class Product extends \Magento\CatalogSampleData\Model\Product
27
30
*/
28
31
protected $ downloadableData = [];
29
32
33
+ /**
34
+ * @var SampleFactory
35
+ */
36
+ protected $ sampleFactory ;
37
+
38
+ /**
39
+ * @var LinkFactory
40
+ */
41
+ protected $ linkFactory ;
42
+
30
43
/**
31
44
* Product constructor.
32
45
* @param SampleDataContext $sampleDataContext
@@ -44,7 +57,9 @@ public function __construct(
44
57
\Magento \DownloadableSampleData \Model \Product \Converter $ converter ,
45
58
\Magento \CatalogSampleData \Model \Product \Gallery $ gallery ,
46
59
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
47
- \Magento \Eav \Model \Config $ eavConfig
60
+ \Magento \Eav \Model \Config $ eavConfig ,
61
+ SampleFactory $ sampleFactory ,
62
+ LinkFactory $ linkFactory
48
63
) {
49
64
parent ::__construct (
50
65
$ sampleDataContext ,
@@ -55,6 +70,8 @@ public function __construct(
55
70
$ storeManager ,
56
71
$ eavConfig
57
72
);
73
+ $ this ->linkFactory = $ linkFactory ;
74
+ $ this ->sampleFactory = $ sampleFactory ;
58
75
}
59
76
60
77
/**
@@ -96,9 +113,109 @@ public function install(array $productFixtures, array $galleryFixtures, array $d
96
113
protected function prepareProduct ($ product , $ data )
97
114
{
98
115
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
+
99
179
$ product ->setDownloadableData ($ this ->downloadableData [$ data ['sku ' ]]);
180
+ $ product ->setExtensionAttributes ($ extension );
100
181
}
101
182
$ this ->setVirtualStockData ($ product );
102
183
return $ this ;
103
184
}
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
+ }
104
221
}
0 commit comments