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
/**
@@ -477,6 +481,7 @@ function (&$item, $key) {
477
481
*
478
482
* @return array
479
483
* @throws ValidatorException
484
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
480
485
*/
481
486
private function getConfigurableProductConfig ()
482
487
{
@@ -500,14 +505,19 @@ private function getConfigurableProductConfig()
500
505
$ skuPull = [];
501
506
foreach ($ configurableProductConfig as $ i => &$ config ) {
502
507
$ attributeSet = $ config ['attributeSet ' ];
503
- $ attributes = ( int ) $ config ['attributes ' ];
508
+ $ attributes = $ config ['attributes ' ];
504
509
$ options = (int )$ config ['options ' ];
505
510
if ($ attributeSet && isset ($ defaultAttributeSets [$ attributeSet ])) {
506
511
// process default attribute sets
507
512
$ attributeSet = $ defaultAttributeSets [$ attributeSet ];
508
513
$ attributes = count ($ attributeSet ['attributes ' ]);
509
514
$ options = count ($ attributeSet ['attributes ' ][0 ]['values ' ]);
515
+ } elseif (is_array ($ attributes )) {
516
+ $ attributeSet = $ this ->getCustomAttributeSet ($ attributes );
517
+ $ options = array_column ($ attributes , 'options ' );
518
+ $ attributes = count ($ attributes );
510
519
} elseif ($ attributes && $ options ) {
520
+ $ attributes = (int )$ attributes ;
511
521
// process dynamic attribute sets
512
522
$ attributeSet = $ this ->getAttributeSet ($ attributes , $ options , $ config ['swatches ' ]);
513
523
}
@@ -522,7 +532,7 @@ private function getConfigurableProductConfig()
522
532
$ config ['attributeSet ' ] = $ this ->convertAttributesToDBFormat ($ attributeSet );
523
533
$ config ['attributes ' ] = $ attributes ;
524
534
$ config ['options ' ] = $ options ;
525
- $ config ['variationCount ' ] = pow ($ options , $ attributes );
535
+ $ config ['variationCount ' ] = is_array ( $ options ) ? array_product ( $ options ) : pow ($ options , $ attributes );
526
536
$ skuPull [] = $ config ['sku ' ];
527
537
}
528
538
@@ -556,6 +566,16 @@ private function prepareConfigurableConfig($defaultAttributeSets)
556
566
} else {
557
567
$ configurableConfigs = $ configurableConfigs ['config ' ];
558
568
}
569
+
570
+ foreach ($ configurableConfigs as &$ config ) {
571
+ if (isset ($ config ['attributes ' ]) && is_array ($ config ['attributes ' ])) {
572
+ if (!isset ($ config ['attributes ' ]['attribute ' ][0 ])) {
573
+ $ config ['attributes ' ] = [$ config ['attributes ' ]['attribute ' ]];
574
+ } else {
575
+ $ config ['attributes ' ] = $ config ['attributes ' ]['attribute ' ];
576
+ }
577
+ }
578
+ }
559
579
} else {
560
580
throw new ValidatorException (__ ('Configurable product config is invalid ' ));
561
581
}
@@ -613,11 +633,10 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
613
633
private function getAttributeSet ($ attributes , $ options , $ swatches )
614
634
{
615
635
$ attributeCode = 'configurable_attribute ' . $ attributes . '_ ' . $ options . '_ ' ;
616
- $ setName = 'Dynamic Attribute Set ' . $ attributes . '- ' . $ options ;
617
636
618
637
return $ this ->attributeSetsFixture ->createAttributeSet (
619
638
$ this ->attributePattern ->generateAttributeSet (
620
- $ setName ,
639
+ $ this -> getAttributeSetName ( $ attributes , $ options ) ,
621
640
$ attributes ,
622
641
$ options ,
623
642
function ($ index , $ attribute ) use ($ attributeCode , $ options , $ swatches ) {
@@ -635,6 +654,68 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
635
654
);
636
655
}
637
656
657
+ /**
658
+ * Provide attribute set based on attributes configuration
659
+ *
660
+ * @param array $attributes
661
+ * @return array
662
+ */
663
+ private function getCustomAttributeSet (array $ attributes )
664
+ {
665
+ $ attributeSetName = $ this ->getAttributeSetName (
666
+ count ($ attributes ),
667
+ implode (', ' , array_column ($ attributes , 'options ' ))
668
+ );
669
+
670
+ $ pattern = $ this ->attributePattern ->generateAttributeSet (
671
+ $ attributeSetName ,
672
+ count ($ attributes ),
673
+ array_column ($ attributes , 'options ' ),
674
+ function ($ index , $ attribute ) use ($ attributeSetName , $ attributes ) {
675
+ $ swatch = [];
676
+ $ timeStamp = time ();
677
+ $ data = [
678
+ 'attribute_code ' => sprintf ('custom_attribute_%s_%s ' , $ index , $ timeStamp ),
679
+ 'frontend_label ' => 'Dynamic Attribute ' . sprintf ('custom_attribute_%s_%s ' , $ index , $ timeStamp ),
680
+ ];
681
+
682
+ if (isset ($ attributes [$ index -1 ]['swatches ' ])) {
683
+ $ data ['is_visible_in_advanced_search ' ] = 1 ;
684
+ $ data ['is_searchable ' ] = 1 ;
685
+ $ data ['is_filterable ' ] = 1 ;
686
+ $ data ['is_filterable_in_search ' ] = 1 ;
687
+ $ data ['used_in_product_listing ' ] = 1 ;
688
+
689
+ $ swatch = $ this ->swatchesGenerator ->generateSwatchData (
690
+ (int ) $ attributes [$ index -1 ]['options ' ],
691
+ $ attributeSetName . $ index ,
692
+ $ attributes [$ index -1 ]['swatches ' ]
693
+ );
694
+ }
695
+
696
+ return array_replace_recursive (
697
+ $ attribute ,
698
+ $ data ,
699
+ $ swatch
700
+ );
701
+ }
702
+ );
703
+
704
+ return $ this ->attributeSetsFixture ->createAttributeSet ($ pattern );
705
+ }
706
+
707
+ /**
708
+ * Provide attribute set name based on amount of attributes and options per attribute set
709
+ *
710
+ * @param int $attributesCount
711
+ * @param int $optionsCount
712
+ * @return string
713
+ */
714
+ private function getAttributeSetName ($ attributesCount , $ optionsCount )
715
+ {
716
+ return sprintf ('Dynamic Attribute Set %s-%s ' , $ attributesCount , $ optionsCount );
717
+ }
718
+
638
719
/**
639
720
* @return array
640
721
*/
@@ -689,13 +770,13 @@ private function getConfigurableProductsVariationsValue()
689
770
*/
690
771
private function getAdditionalAttributesClosure (array $ attributes , $ variationCount )
691
772
{
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 ;
773
+ $ optionsPerAttribute = array_map (function ($ attr ) {
774
+ return count ($ attr ['values ' ]);
775
+ }, $ attributes );
776
+ $ variationsMatrix = $ this ->generateVariationsMatrix (count ($ attributes ), $ optionsPerAttribute );
697
777
698
- $ variationsMatrix = $ this ->generateVariationsMatrix (count ($ attributes ), $ optionsPerAttribute );
778
+ return function ($ attributeSetId , $ index , $ entityNumber ) use ($ attributes , $ variationCount , $ variationsMatrix ) {
779
+ $ variationIndex = $ this ->getConfigurableVariationIndex ($ entityNumber , $ variationCount ) - 1 ;
699
780
if (isset ($ variationsMatrix [$ variationIndex ])) {
700
781
$ tempProductData = [];
701
782
foreach ($ variationsMatrix [$ variationIndex ] as $ attributeIndex => $ optionIndex ) {
@@ -706,7 +787,7 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
706
787
return $ tempProductData ;
707
788
}
708
789
709
- return $ attributeValues ;
790
+ return [] ;
710
791
};
711
792
}
712
793
@@ -719,8 +800,8 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
719
800
private function generateVariationsMatrix ($ attributesPerSet , $ optionsPerAttribute )
720
801
{
721
802
$ variationsMatrix = null ;
722
- for ($ i = 1 ; $ i <= $ attributesPerSet ; $ i ++) {
723
- $ variationsMatrix [] = range (0 , $ optionsPerAttribute - 1 );
803
+ for ($ i = 0 ; $ i < $ attributesPerSet ; $ i ++) {
804
+ $ variationsMatrix [] = range (0 , $ optionsPerAttribute[ $ i ] - 1 );
724
805
}
725
806
return $ this ->generateVariations ($ variationsMatrix );
726
807
}
0 commit comments