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