47
47
* </config>
48
48
* </configurable_products>
49
49
*
50
+ * 2.3 Generate products based on dynamically created attribute set with specified configuration per each attribute
51
+ * <configurable_products> <!-- Configurable product -->
52
+ * <config>
53
+ * <attributes>
54
+ * <!-- Configuration for a first attribute -->
55
+ * <attribute>
56
+ * <options>{Amount of options per attribute}</options>
57
+ * <swatches>color|image</swatches> Type of Swatch attribute
58
+ * </attribute>
59
+ * <!-- Configuration for a second attribute -->
60
+ * <attribute>
61
+ * <options>{Amount of options per attribute}</options>
62
+ * </attribute>
63
+ * </attributes>
64
+ * <sku>{Configurable sku pattern with %s}</sku>
65
+ * <products>{Amount of configurable products}</products>
66
+ * </config>
67
+ * </configurable_products>
68
+ *
50
69
* Products will be uniformly distributed per categories and websites
51
70
* If node "assign_entities_to_all_websites" from profile is set to "1" then products will be assigned to all websites
52
71
*
@@ -392,31 +411,16 @@ public function introduceParamLabels()
392
411
*/
393
412
public function printInfo (OutputInterface $ output )
394
413
{
395
- $ config = $ this ->fixtureModel ->getValue ('configurable_products ' , []);
396
- if (!$ config ) {
414
+ if (!$ this ->fixtureModel ->getValue ('configurable_products ' , [])) {
397
415
return ;
398
416
}
399
417
400
- $ generalAmount = is_numeric ($ config ) ? $ config : array_sum (array_column ($ config , 'products ' ));
401
- $ output ->writeln (sprintf ('<info> |- Configurable products: %s</info> ' , $ generalAmount ));
402
-
403
418
$ configurableProductConfig = $ this ->prepareConfigurableConfig (
404
419
$ this ->getDefaultAttributeSetsWithAttributes ()
405
420
);
421
+ $ generalAmount = array_sum (array_column ($ configurableProductConfig , 'products ' ));
406
422
407
- foreach ($ configurableProductConfig as $ config ) {
408
- $ attributeSetName = isset ($ config ['attributeSet ' ])
409
- ? $ config ['attributeSet ' ]
410
- : 'Dynamic Attribute Set ' . $ config ['attributes ' ] . '- ' . $ config ['options ' ];
411
-
412
- $ output ->writeln (
413
- sprintf (
414
- '<info> |--- %s products for attribute set "%s"</info> ' ,
415
- $ config ['products ' ],
416
- $ attributeSetName
417
- )
418
- );
419
- }
423
+ $ output ->writeln (sprintf ('<info> |- Configurable products: %s</info> ' , $ generalAmount ));
420
424
}
421
425
422
426
/**
@@ -500,14 +504,19 @@ private function getConfigurableProductConfig()
500
504
$ skuPull = [];
501
505
foreach ($ configurableProductConfig as $ i => &$ config ) {
502
506
$ attributeSet = $ config ['attributeSet ' ];
503
- $ attributes = ( int ) $ config ['attributes ' ];
507
+ $ attributes = $ config ['attributes ' ];
504
508
$ options = (int )$ config ['options ' ];
505
509
if ($ attributeSet && isset ($ defaultAttributeSets [$ attributeSet ])) {
506
510
// process default attribute sets
507
511
$ attributeSet = $ defaultAttributeSets [$ attributeSet ];
508
512
$ attributes = count ($ attributeSet ['attributes ' ]);
509
513
$ options = count ($ attributeSet ['attributes ' ][0 ]['values ' ]);
514
+ } elseif (is_array ($ attributes )) {
515
+ $ attributeSet = $ this ->getCustomAttributeSet ($ attributes );
516
+ $ options = array_column ($ attributes , 'options ' );
517
+ $ attributes = count ($ attributes );
510
518
} elseif ($ attributes && $ options ) {
519
+ $ attributes = (int )$ attributes ;
511
520
// process dynamic attribute sets
512
521
$ attributeSet = $ this ->getAttributeSet ($ attributes , $ options , $ config ['swatches ' ]);
513
522
}
@@ -522,7 +531,7 @@ private function getConfigurableProductConfig()
522
531
$ config ['attributeSet ' ] = $ this ->convertAttributesToDBFormat ($ attributeSet );
523
532
$ config ['attributes ' ] = $ attributes ;
524
533
$ config ['options ' ] = $ options ;
525
- $ config ['variationCount ' ] = pow ($ options , $ attributes );
534
+ $ config ['variationCount ' ] = is_array ( $ options ) ? array_product ( $ options ) : pow ($ options , $ attributes );
526
535
$ skuPull [] = $ config ['sku ' ];
527
536
}
528
537
@@ -556,6 +565,16 @@ private function prepareConfigurableConfig($defaultAttributeSets)
556
565
} else {
557
566
$ configurableConfigs = $ configurableConfigs ['config ' ];
558
567
}
568
+
569
+ foreach ($ configurableConfigs as &$ config ) {
570
+ if (isset ($ config ['attributes ' ]) && is_array ($ config ['attributes ' ])) {
571
+ if (!isset ($ config ['attributes ' ]['attribute ' ][0 ])) {
572
+ $ config ['attributes ' ] = [$ config ['attributes ' ]['attribute ' ]];
573
+ } else {
574
+ $ config ['attributes ' ] = $ config ['attributes ' ]['attribute ' ];
575
+ }
576
+ }
577
+ }
559
578
} else {
560
579
throw new ValidatorException (__ ('Configurable product config is invalid ' ));
561
580
}
@@ -613,11 +632,10 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
613
632
private function getAttributeSet ($ attributes , $ options , $ swatches )
614
633
{
615
634
$ attributeCode = 'configurable_attribute ' . $ attributes . '_ ' . $ options . '_ ' ;
616
- $ setName = 'Dynamic Attribute Set ' . $ attributes . '- ' . $ options ;
617
635
618
636
return $ this ->attributeSetsFixture ->createAttributeSet (
619
637
$ this ->attributePattern ->generateAttributeSet (
620
- $ setName ,
638
+ $ this -> getAttributeSetName ( $ attributes , $ options ) ,
621
639
$ attributes ,
622
640
$ options ,
623
641
function ($ index , $ attribute ) use ($ attributeCode , $ options , $ swatches ) {
@@ -635,6 +653,55 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
635
653
);
636
654
}
637
655
656
+ private function getCustomAttributeSet (array $ attributes )
657
+ {
658
+ $ attributeSetName = $ this ->getAttributeSetName (
659
+ count ($ attributes ),
660
+ implode (', ' , array_column ($ attributes , 'options ' ))
661
+ );
662
+
663
+ $ pattern = $ this ->attributePattern ->generateAttributeSet (
664
+ $ attributeSetName ,
665
+ count ($ attributes ),
666
+ array_column ($ attributes , 'options ' ),
667
+ function ($ index , $ attribute ) use ($ attributeSetName , $ attributes ) {
668
+ $ swatch = [];
669
+ $ timeStamp = time ();
670
+ $ data = [
671
+ 'attribute_code ' => sprintf ('custom_attribute_%s_%s ' , $ index , $ timeStamp ),
672
+ 'frontend_label ' => 'Dynamic Attribute ' . sprintf ('custom_attribute_%s_%s ' , $ index , $ timeStamp ),
673
+ ];
674
+
675
+ if (isset ($ attributes [$ index -1 ]['swatches ' ])) {
676
+ $ data ['is_visible_in_advanced_search ' ] = 1 ;
677
+ $ data ['is_searchable ' ] = 1 ;
678
+ $ data ['is_filterable ' ] = 1 ;
679
+ $ data ['is_filterable_in_search ' ] = 1 ;
680
+ $ data ['used_in_product_listing ' ] = 1 ;
681
+
682
+ $ swatch = $ this ->swatchesGenerator ->generateSwatchData (
683
+ (int ) $ attributes [$ index -1 ]['options ' ],
684
+ $ attributeSetName . $ index ,
685
+ $ attributes [$ index -1 ]['swatches ' ]
686
+ );
687
+ }
688
+
689
+ return array_replace_recursive (
690
+ $ attribute ,
691
+ $ data ,
692
+ $ swatch
693
+ );
694
+ }
695
+ );
696
+
697
+ return $ this ->attributeSetsFixture ->createAttributeSet ($ pattern );
698
+ }
699
+
700
+ private function getAttributeSetName ($ attributesCount , $ optionsCount )
701
+ {
702
+ return sprintf ('Dynamic Attribute Set %s-%s ' , $ attributesCount , $ optionsCount );
703
+ }
704
+
638
705
/**
639
706
* @return array
640
707
*/
@@ -689,13 +756,13 @@ private function getConfigurableProductsVariationsValue()
689
756
*/
690
757
private function getAdditionalAttributesClosure (array $ attributes , $ variationCount )
691
758
{
692
- return function ($ attributeSetId , $ index , $ entityNumber ) use ($ attributes , $ variationCount ) {
693
- $ variationIndex = $ this ->getConfigurableVariationIndex ($ entityNumber , $ variationCount ) - 1 ;
694
- $ attributeValues = [];
695
- $ optionsPerAttribute = count ($ attributes [0 ]['values ' ]);
696
- $ variationIndex = $ variationIndex % $ variationCount ;
759
+ $ optionsPerAttribute = array_map (function ($ attr ) {
760
+ return count ($ attr ['values ' ]);
761
+ }, $ attributes );
762
+ $ variationsMatrix = $ this ->generateVariationsMatrix (count ($ attributes ), $ optionsPerAttribute );
697
763
698
- $ variationsMatrix = $ this ->generateVariationsMatrix (count ($ attributes ), $ optionsPerAttribute );
764
+ return function ($ attributeSetId , $ index , $ entityNumber ) use ($ attributes , $ variationCount , $ variationsMatrix ) {
765
+ $ variationIndex = $ this ->getConfigurableVariationIndex ($ entityNumber , $ variationCount ) - 1 ;
699
766
if (isset ($ variationsMatrix [$ variationIndex ])) {
700
767
$ tempProductData = [];
701
768
foreach ($ variationsMatrix [$ variationIndex ] as $ attributeIndex => $ optionIndex ) {
@@ -706,7 +773,7 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
706
773
return $ tempProductData ;
707
774
}
708
775
709
- return $ attributeValues ;
776
+ return [] ;
710
777
};
711
778
}
712
779
@@ -719,8 +786,8 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
719
786
private function generateVariationsMatrix ($ attributesPerSet , $ optionsPerAttribute )
720
787
{
721
788
$ variationsMatrix = null ;
722
- for ($ i = 1 ; $ i <= $ attributesPerSet ; $ i ++) {
723
- $ variationsMatrix [] = range (0 , $ optionsPerAttribute - 1 );
789
+ for ($ i = 0 ; $ i < $ attributesPerSet ; $ i ++) {
790
+ $ variationsMatrix [] = range (0 , $ optionsPerAttribute[ $ i ] - 1 );
724
791
}
725
792
return $ this ->generateVariations ($ variationsMatrix );
726
793
}
0 commit comments