1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\DataFixtures\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Sylius\Bundle\CoreBundle\DataFixtures\DataFixture; |
17
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
18
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
19
|
|
|
use Sylius\Component\Product\Model\AttributeValueInterface; |
20
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Default assortment products to play with Sylius. |
24
|
|
|
* |
25
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
26
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class LoadProductsData extends DataFixture |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Total variants created. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $totalVariants = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function load(ObjectManager $manager) |
41
|
|
|
{ |
42
|
|
|
for ($i = 1; $i <= 30; ++$i) { |
43
|
|
|
switch (rand(0, 3)) { |
44
|
|
|
case 0: |
45
|
|
|
$manager->persist($this->createTShirt($i)); |
46
|
|
|
break; |
47
|
|
|
|
48
|
|
|
case 1: |
49
|
|
|
$manager->persist($this->createSticker($i)); |
50
|
|
|
break; |
51
|
|
|
|
52
|
|
|
case 2: |
53
|
|
|
$manager->persist($this->createMug($i)); |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
case 3: |
57
|
|
|
$manager->persist($this->createBook($i)); |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (0 === $i % 10) { |
62
|
|
|
$manager->flush(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$manager->flush(); |
67
|
|
|
|
68
|
|
|
$this->defineTotalVariants(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getOrder() |
75
|
|
|
{ |
76
|
|
|
return 50; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $i |
81
|
|
|
* |
82
|
|
|
* @return ProductInterface |
83
|
|
|
*/ |
84
|
|
|
protected function createTShirt($i) |
85
|
|
|
{ |
86
|
|
|
$product = $this->createProduct(); |
87
|
|
|
|
88
|
|
|
$translatedNames = [ |
89
|
|
|
$this->defaultLocale => sprintf('T-Shirt "%s"', $this->faker->word), |
90
|
|
|
'es_ES' => sprintf('Camiseta "%s"', $this->fakers['es_ES']->word), |
91
|
|
|
]; |
92
|
|
|
$this->addTranslatedFields($product, $translatedNames); |
93
|
|
|
$product->setCode($this->faker->uuid); |
94
|
|
|
|
95
|
|
|
$product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_MATCH); |
96
|
|
|
|
97
|
|
|
$this->setTaxons($product, ['t-shirts', 'super_tees']); |
98
|
|
|
$product->setMainTaxon($this->getReference('Sylius.Taxon.t-shirts')); |
99
|
|
|
$product->setArchetype($this->getReference('Sylius.Archetype.t_shirt')); |
100
|
|
|
|
101
|
|
|
$this->addVariant($product); |
102
|
|
|
$this->setChannels($product, ['DEFAULT']); |
103
|
|
|
|
104
|
|
|
// T-Shirt brand. |
105
|
|
|
$randomBrand = $this->faker->randomElement(['Nike', 'Adidas', 'Puma', 'Potato']); |
|
|
|
|
106
|
|
|
$this->addAttribute($product, 't_shirt_brand', $randomBrand); |
107
|
|
|
|
108
|
|
|
// T-Shirt collection. |
109
|
|
|
$randomCollection = sprintf('Symfony2 %s %s', $this->faker->randomElement(['Summer', 'Winter', 'Spring', 'Autumn']), rand(1995, 2012)); |
|
|
|
|
110
|
|
|
$this->addAttribute($product, 't_shirt_collection', $randomCollection); |
111
|
|
|
|
112
|
|
|
// T-Shirt material. |
113
|
|
|
$randomMaterial = $this->faker->randomElement(['Polyester', 'Wool', 'Polyester 10% / Wool 90%', 'Potato 100%']); |
|
|
|
|
114
|
|
|
$this->addAttribute($product, 't_shirt_material', $randomMaterial); |
115
|
|
|
|
116
|
|
|
$product->addOption($this->getReference('Sylius.Option.t_shirt_size')); |
117
|
|
|
$product->addOption($this->getReference('Sylius.Option.t_shirt_color')); |
118
|
|
|
|
119
|
|
|
$this->generateVariants($product); |
120
|
|
|
|
121
|
|
|
$this->setReference('Sylius.Product.'.$i, $product); |
122
|
|
|
|
123
|
|
|
return $product; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create sticker product. |
128
|
|
|
* |
129
|
|
|
* @param int $i |
130
|
|
|
* |
131
|
|
|
* @return ProductInterface |
132
|
|
|
*/ |
133
|
|
|
protected function createSticker($i) |
134
|
|
|
{ |
135
|
|
|
$product = $this->createProduct(); |
136
|
|
|
|
137
|
|
|
$translatedNames = [ |
138
|
|
|
$this->defaultLocale => sprintf('Sticker "%s"', $this->faker->word), |
139
|
|
|
'es_ES' => sprintf('Pegatina "%s"', $this->fakers['es_ES']->word), |
140
|
|
|
]; |
141
|
|
|
$this->addTranslatedFields($product, $translatedNames); |
142
|
|
|
$product->setCode($this->faker->uuid); |
143
|
|
|
|
144
|
|
|
$product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_MATCH); |
145
|
|
|
|
146
|
|
|
$this->setTaxons($product, ['stickers', 'stickypicky']); |
147
|
|
|
$product->setMainTaxon($this->getReference('Sylius.Taxon.stickers')); |
148
|
|
|
$product->setArchetype($this->getReference('Sylius.Archetype.sticker')); |
149
|
|
|
|
150
|
|
|
$this->addVariant($product); |
151
|
|
|
$this->setChannels($product, ['DEFAULT']); |
152
|
|
|
|
153
|
|
|
// Sticker resolution. |
154
|
|
|
$randomResolution = $this->faker->randomElement(['Waka waka', 'FULL HD', '300DPI', '200DPI']); |
|
|
|
|
155
|
|
|
$this->addAttribute($product, 'sticker_resolution', $randomResolution); |
156
|
|
|
|
157
|
|
|
// Sticker paper. |
158
|
|
|
$randomPaper = sprintf('Paper from tree %s', $this->faker->randomElement(['Wung', 'Yang', 'Lemon-San', 'Me-Gusta'])); |
|
|
|
|
159
|
|
|
$this->addAttribute($product, 'sticker_paper', $randomPaper); |
160
|
|
|
|
161
|
|
|
$product->addOption($this->getReference('Sylius.Option.sticker_size')); |
162
|
|
|
|
163
|
|
|
$this->generateVariants($product); |
164
|
|
|
|
165
|
|
|
$this->setReference('Sylius.Product.'.$i, $product); |
166
|
|
|
|
167
|
|
|
return $product; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Create mug product. |
172
|
|
|
* |
173
|
|
|
* @param int $i |
174
|
|
|
* |
175
|
|
|
* @return ProductInterface |
176
|
|
|
*/ |
177
|
|
|
protected function createMug($i) |
178
|
|
|
{ |
179
|
|
|
$product = $this->createProduct(); |
180
|
|
|
|
181
|
|
|
$translatedNames = [ |
182
|
|
|
$this->defaultLocale => sprintf('Mug "%s"', $this->faker->word), |
183
|
|
|
'es_ES' => sprintf('Taza "%s"', $this->fakers['es_ES']->word), |
184
|
|
|
]; |
185
|
|
|
$this->addTranslatedFields($product, $translatedNames); |
186
|
|
|
$product->setCode($this->faker->uuid); |
187
|
|
|
|
188
|
|
|
$this->setTaxons($product, ['mugs', 'mugland']); |
189
|
|
|
$product->setMainTaxon($this->getReference('Sylius.Taxon.mugs')); |
190
|
|
|
$product->setArchetype($this->getReference('Sylius.Archetype.mug')); |
191
|
|
|
|
192
|
|
|
$this->addVariant($product); |
193
|
|
|
$this->setChannels($product, ['DEFAULT']); |
194
|
|
|
|
195
|
|
|
$randomMugMaterial = $this->faker->randomElement(['Invisible porcelain', 'Banana skin', 'Porcelain', 'Sand']); |
|
|
|
|
196
|
|
|
$this->addAttribute($product, 'mug_material', $randomMugMaterial); |
197
|
|
|
|
198
|
|
|
$product->addOption($this->getReference('Sylius.Option.mug_type')); |
199
|
|
|
|
200
|
|
|
$this->generateVariants($product); |
201
|
|
|
|
202
|
|
|
$this->setReference('Sylius.Product.'.$i, $product); |
203
|
|
|
|
204
|
|
|
return $product; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Create book product. |
209
|
|
|
* |
210
|
|
|
* @param int $i |
211
|
|
|
* |
212
|
|
|
* @return ProductInterface |
213
|
|
|
*/ |
214
|
|
|
protected function createBook($i) |
215
|
|
|
{ |
216
|
|
|
$product = $this->createProduct(); |
217
|
|
|
|
218
|
|
|
$author = $this->faker->name; |
219
|
|
|
$isbn = $this->getUniqueISBN(); |
220
|
|
|
|
221
|
|
|
$translatedNames = [ |
222
|
|
|
$this->defaultLocale => sprintf('Book "%s" by "%s"', ucfirst($this->faker->word), $author), |
223
|
|
|
'es_ES' => sprintf('Libro "%s" de "%s"', ucfirst($this->fakers['es_ES']->word), $author), |
224
|
|
|
]; |
225
|
|
|
$this->addTranslatedFields($product, $translatedNames); |
226
|
|
|
$product->setCode($this->faker->uuid); |
227
|
|
|
|
228
|
|
|
$this->setTaxons($product, ['books', 'bookmania']); |
229
|
|
|
$product->setMainTaxon($this->getReference('Sylius.Taxon.books')); |
230
|
|
|
$product->setArchetype($this->getReference('Sylius.Archetype.book')); |
231
|
|
|
|
232
|
|
|
$this->addVariant($product, $isbn); |
233
|
|
|
$this->setChannels($product, ['DEFAULT']); |
234
|
|
|
|
235
|
|
|
$this->addAttribute($product, 'book_author', $author); |
236
|
|
|
$this->addAttribute($product, 'book_isbn', $isbn); |
237
|
|
|
$this->addAttribute($product, 'book_pages', $this->faker->randomNumber(3)); |
238
|
|
|
|
239
|
|
|
$this->setReference('Sylius.Product.'.$i, $product); |
240
|
|
|
|
241
|
|
|
return $product; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generates all possible variants with random prices. |
246
|
|
|
* |
247
|
|
|
* @param ProductInterface $product |
248
|
|
|
*/ |
249
|
|
|
protected function generateVariants(ProductInterface $product) |
250
|
|
|
{ |
251
|
|
|
$this |
252
|
|
|
->getVariantGenerator() |
253
|
|
|
->generate($product) |
254
|
|
|
; |
255
|
|
|
|
256
|
|
|
foreach ($product->getVariants() as $variant) { |
257
|
|
|
$variant->setAvailableOn($this->faker->dateTimeThisYear); |
258
|
|
|
$variant->setPrice($this->faker->randomNumber(4)); |
259
|
|
|
$variant->setCode($this->getUniqueCode()); |
260
|
|
|
$variant->setOnHand($this->faker->randomNumber(1)); |
261
|
|
|
|
262
|
|
|
$this->setReference('Sylius.Variant-'.$this->totalVariants, $variant); |
263
|
|
|
|
264
|
|
|
++$this->totalVariants; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param ProductInterface $product |
270
|
|
|
* @param string $code |
271
|
|
|
*/ |
272
|
|
|
protected function addVariant(ProductInterface $product, $code = null) |
273
|
|
|
{ |
274
|
|
|
/** @var ProductVariantInterface $variant */ |
275
|
|
|
$variant = $this->get('sylius.factory.product_variant')->createNew(); |
276
|
|
|
$variant->setProduct($product); |
277
|
|
|
$variant->setPrice($this->faker->randomNumber(4)); |
278
|
|
|
$variant->setCode(null === $code ? $this->getUniqueCode() : $code); |
279
|
|
|
$variant->setAvailableOn($this->faker->dateTimeThisYear); |
280
|
|
|
$variant->setOnHand($this->faker->randomNumber(1)); |
281
|
|
|
$variant->setTaxCategory($this->getTaxCategory('taxable')); |
282
|
|
|
|
283
|
|
|
$mainTaxon = $product->getMainTaxon(); |
284
|
|
|
$image = clone $this->getReference('Sylius.Image.'.$mainTaxon->getCode()); |
285
|
|
|
$variant->addImage($image); |
286
|
|
|
|
287
|
|
|
$product->addVariant($variant); |
288
|
|
|
|
289
|
|
|
$this->setReference('Sylius.Variant-'.$this->totalVariants, $variant); |
290
|
|
|
|
291
|
|
|
++$this->totalVariants; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Adds attribute to product with given value. |
296
|
|
|
* |
297
|
|
|
* @param ProductInterface $product |
298
|
|
|
* @param string $code |
299
|
|
|
* @param string $value |
300
|
|
|
*/ |
301
|
|
|
protected function addAttribute(ProductInterface $product, $code, $value) |
302
|
|
|
{ |
303
|
|
|
/* @var $attribute AttributeValueInterface */ |
304
|
|
|
$attribute = $this->getProductAttributeValueFactory()->createNew(); |
|
|
|
|
305
|
|
|
$attribute->setAttribute($this->getReference('Sylius.Attribute.'.$code)); |
306
|
|
|
$attribute->setProduct($product); |
307
|
|
|
$attribute->setValue($value); |
308
|
|
|
|
309
|
|
|
$product->addAttribute($attribute); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Adds taxons to given product. |
314
|
|
|
* |
315
|
|
|
* @param ProductInterface $product |
316
|
|
|
* @param array $taxonCodes |
317
|
|
|
*/ |
318
|
|
|
protected function setTaxons(ProductInterface $product, array $taxonCodes) |
319
|
|
|
{ |
320
|
|
|
$taxons = new ArrayCollection(); |
321
|
|
|
|
322
|
|
|
foreach ($taxonCodes as $taxonCode) { |
323
|
|
|
$taxons->add($this->getReference('Sylius.Taxon.'.$taxonCode)); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$product->setTaxons($taxons); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Set channels. |
331
|
|
|
* |
332
|
|
|
* @param ProductInterface $product |
333
|
|
|
* @param array $channelCodes |
334
|
|
|
*/ |
335
|
|
|
protected function setChannels(ProductInterface $product, array $channelCodes) |
336
|
|
|
{ |
337
|
|
|
foreach ($channelCodes as $code) { |
338
|
|
|
$product->addChannel($this->getReference('Sylius.Channel.'.$code)); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param string $code |
344
|
|
|
* |
345
|
|
|
* @return TaxCategoryInterface |
346
|
|
|
*/ |
347
|
|
|
protected function getTaxCategory($code) |
348
|
|
|
{ |
349
|
|
|
return $this->getReference('Sylius.TaxCategory.'.$code); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get unique SKU. |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
|
|
protected function getUniqueCode() |
358
|
|
|
{ |
359
|
|
|
return $this->faker->unique()->uuid(); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get unique ISBN number. |
364
|
|
|
* |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
protected function getUniqueISBN() |
368
|
|
|
{ |
369
|
|
|
return $this->faker->unique()->uuid(); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return ProductInterface |
374
|
|
|
*/ |
375
|
|
|
protected function createProduct() |
376
|
|
|
{ |
377
|
|
|
return $this->getProductFactory()->createNew(); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Define constant with number of total variants created. |
382
|
|
|
*/ |
383
|
|
|
protected function defineTotalVariants() |
384
|
|
|
{ |
385
|
|
|
define('SYLIUS_FIXTURES_TOTAL_VARIANTS', $this->totalVariants); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
protected function addTranslatedFields(ProductInterface $product, $translatedNames) |
389
|
|
|
{ |
390
|
|
|
foreach ($translatedNames as $locale => $name) { |
391
|
|
|
$product->setCurrentLocale($locale); |
392
|
|
|
$product->setFallbackLocale($locale); |
393
|
|
|
|
394
|
|
|
$product->setName($name); |
395
|
|
|
$product->setDescription($this->fakers[$locale]->paragraph); |
396
|
|
|
$product->setShortDescription($this->fakers[$locale]->sentence); |
397
|
|
|
$product->setMetaKeywords(str_replace(' ', ', ', $this->fakers[$locale]->sentence)); |
398
|
|
|
$product->setMetaDescription($this->fakers[$locale]->sentence); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$product->setCurrentLocale($this->defaultLocale); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|