9
9
use Magento \Catalog \Model \Product ;
10
10
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11
11
use Magento \Catalog \Model \ProductFactory ;
12
+ use Magento \Eav \Model \Entity \Attribute ;
13
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Collection ;
12
14
use Magento \Eav \Model \ResourceModel \Entity \Attribute \CollectionFactory ;
15
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \Collection as AttributeSetCollection ;
13
16
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory as AttributeSetCollectionFactory ;
14
17
use Magento \Setup \Model \FixtureGenerator \ProductGenerator ;
15
18
use Magento \Setup \Model \SearchTermDescriptionGeneratorFactory ;
@@ -68,7 +71,7 @@ class SimpleProductsFixture extends Fixture
68
71
private $ defaultAttributeSetId ;
69
72
70
73
/**
71
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\ Collection
74
+ * @var Collection
72
75
*/
73
76
private $ attributeCollectionFactory ;
74
77
@@ -97,6 +100,11 @@ class SimpleProductsFixture extends Fixture
97
100
*/
98
101
private $ priceProvider ;
99
102
103
+ /**
104
+ * @var int[]
105
+ */
106
+ private $ additionalAttributeSetIds ;
107
+
100
108
/**
101
109
* @param FixtureModel $fixtureModel
102
110
* @param ProductFactory $productFactory
@@ -184,35 +192,38 @@ public function execute()
184
192
'Short simple product Description %s '
185
193
);
186
194
187
- $ additionalAttributeSets = $ this ->getAdditionalAttributeSets ();
188
- $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSets ) {
195
+ $ additionalAttributeSetIds = $ this ->getAdditionalAttributeSetIds ();
196
+ $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSetIds ) {
189
197
// phpcs:ignore
190
198
mt_srand ($ index );
191
199
$ attributeSetCount = count (array_keys ($ defaultAttributeSets ));
192
200
if ($ attributeSetCount > (($ index - 1 ) % (int )$ this ->fixtureModel ->getValue ('categories ' , 30 ))) {
193
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
201
+ // phpcs:ignore Magento2.Security.InsecureFunction
194
202
return array_keys ($ defaultAttributeSets )[mt_rand (0 , count (array_keys ($ defaultAttributeSets )) - 1 )];
195
203
} else {
196
- $ customSetsAmount = count ($ additionalAttributeSets );
204
+ $ customSetsAmount = count ($ additionalAttributeSetIds );
197
205
return $ customSetsAmount
198
- ? $ additionalAttributeSets [$ index % count ( $ additionalAttributeSets )][ ' attribute_set_id ' ]
206
+ ? $ additionalAttributeSetIds [$ index % $ customSetsAmount ]
199
207
: $ this ->getDefaultAttributeSetId ();
200
208
}
201
209
};
202
210
211
+ $ additionalAttributeValues = $ this ->getAdditionalAttributeValues ();
203
212
$ additionalAttributes = function (
204
213
$ attributeSetId ,
205
214
$ index
206
215
) use (
207
216
$ defaultAttributeSets ,
208
- $ additionalAttributeSets
217
+ $ additionalAttributeValues
209
218
) {
210
219
$ attributeValues = [];
211
220
// phpcs:ignore
212
221
mt_srand ($ index );
213
- if (isset ($ defaultAttributeSets [$ attributeSetId ])) {
214
- foreach ($ defaultAttributeSets [$ attributeSetId ] as $ attributeCode => $ values ) {
215
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
222
+ $ attributeValuesByAttributeSet = $ defaultAttributeSets [$ attributeSetId ]
223
+ ?? $ additionalAttributeValues [$ attributeSetId ];
224
+ if (!empty ($ attributeValuesByAttributeSet )) {
225
+ foreach ($ attributeValuesByAttributeSet as $ attributeCode => $ values ) {
226
+ // phpcs:ignore Magento2.Security.InsecureFunction
216
227
$ attributeValues [$ attributeCode ] = $ values [mt_rand (0 , count ($ values ) - 1 )];
217
228
}
218
229
}
@@ -279,10 +290,10 @@ private function getDefaultAttributeSetId()
279
290
}
280
291
281
292
/**
282
- * Get default attribute sets with attributes
293
+ * Get default attribute sets with attributes.
283
294
*
284
- * @see config/attributeSets.xml
285
295
* @return array
296
+ * @see config/attributeSets.xml
286
297
*/
287
298
private function getDefaultAttributeSets ()
288
299
{
@@ -301,17 +312,7 @@ private function getDefaultAttributeSets()
301
312
'attribute_code ' ,
302
313
array_column ($ attributesData , 'attribute_code ' )
303
314
);
304
- /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
305
- foreach ($ attributeCollection as $ attribute ) {
306
- $ values = [];
307
- $ options = $ attribute ->getOptions ();
308
- foreach (($ options ?: []) as $ option ) {
309
- if ($ option ->getValue ()) {
310
- $ values [] = $ option ->getValue ();
311
- }
312
- }
313
- $ attributes [$ attribute ->getAttributeSetId ()][$ attribute ->getAttributeCode ()] = $ values ;
314
- }
315
+ $ attributes = $ this ->processAttributeValues ($ attributeCollection , $ attributes );
315
316
}
316
317
}
317
318
$ attributes [$ this ->getDefaultAttributeSetId ()] = [];
@@ -381,16 +382,64 @@ private function readDescriptionConfig($configSrc)
381
382
}
382
383
383
384
/**
384
- * Get additional attribute sets
385
+ * Get additional attribute set ids.
386
+ *
387
+ * @return int[]
388
+ */
389
+ private function getAdditionalAttributeSetIds ()
390
+ {
391
+ if (null === $ this ->additionalAttributeSetIds ) {
392
+ /** @var AttributeSetCollection $sets */
393
+ $ sets = $ this ->attributeSetCollectionFactory ->create ();
394
+ $ sets ->addFieldToFilter (
395
+ 'attribute_set_name ' ,
396
+ ['like ' => AttributeSetsFixture::PRODUCT_SET_NAME . '% ' ]
397
+ );
398
+ $ this ->additionalAttributeSetIds = $ sets ->getAllIds ();
399
+ }
400
+
401
+ return $ this ->additionalAttributeSetIds ;
402
+ }
403
+
404
+ /**
405
+ * Get values of additional attributes.
385
406
*
386
- * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection[]
407
+ * @return array
387
408
*/
388
- private function getAdditionalAttributeSets ()
409
+ private function getAdditionalAttributeValues (): array
389
410
{
390
- /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $sets */
391
- $ sets = $ this ->attributeSetCollectionFactory ->create ();
392
- $ sets ->addFieldToFilter ('attribute_set_name ' , ['like ' => AttributeSetsFixture::PRODUCT_SET_NAME . '% ' ]);
411
+ $ attributeCollection = $ this ->attributeCollectionFactory ->create ();
412
+ $ attributeCollection ->setAttributeSetsFilter ($ this ->getAdditionalAttributeSetIds ())
413
+ ->addFieldToFilter ('attribute_code ' , ['like ' => 'attribute_set% ' ]);
414
+ $ attributeCollection ->getSelect ()->columns (['entity_attribute.attribute_set_id ' ]);
415
+
416
+ return $ this ->processAttributeValues ($ attributeCollection );
417
+ }
393
418
394
- return $ sets ->getData ();
419
+ /**
420
+ * Maps attribute values by attribute set and attribute code.
421
+ *
422
+ * @param Collection $attributeCollection
423
+ * @param array $attributes
424
+ * @return array
425
+ */
426
+ private function processAttributeValues (
427
+ Collection $ attributeCollection ,
428
+ array $ attributes = []
429
+ ): array {
430
+ /** @var Attribute $attribute */
431
+ foreach ($ attributeCollection as $ attribute ) {
432
+ $ values = [];
433
+ $ options = $ attribute ->getOptions () ?? [];
434
+ $ attributeSetId = $ attribute ->getAttributeSetId () ?? $ this ->getDefaultAttributeSetId ();
435
+ foreach ($ options as $ option ) {
436
+ if ($ option ->getValue ()) {
437
+ $ values [] = $ option ->getValue ();
438
+ }
439
+ }
440
+ $ attributes [$ attributeSetId ][$ attribute ->getAttributeCode ()] = $ values ;
441
+ }
442
+
443
+ return $ attributes ;
395
444
}
396
445
}
0 commit comments