10
10
use Magento \Catalog \Model \Product \Visibility ;
11
11
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
12
12
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
13
+ use Magento \Eav \Model \Entity \Attribute ;
13
14
use Magento \Eav \Model \ResourceModel \Entity \Attribute \CollectionFactory ;
14
15
use Magento \Framework \Exception \ValidatorException ;
15
16
use Magento \Setup \Model \DataGenerator ;
@@ -518,9 +519,14 @@ private function getConfigurableProductConfig()
518
519
$ attributes = count ($ attributes );
519
520
} elseif ($ attributes && $ options ) {
520
521
$ attributes = (int )$ attributes ;
521
- // process dynamic attribute sets
522
- $ attributeSet = $ this ->getAttributeSet ($ attributes , $ options , $ config ['swatches ' ]);
522
+ // convert attributes and options to array for process custom attribute set creation
523
+ $ attributesData = array_map (function ($ options ) use ($ config ) {
524
+ return ['options ' => $ options , 'swatches ' => $ config ['swatches ' ]];
525
+ }, array_fill (0 , $ attributes , $ options ));
526
+
527
+ $ attributeSet = $ this ->getCustomAttributeSet ($ attributesData );
523
528
}
529
+
524
530
// do not process if any required option is missed
525
531
if (count (array_filter ([$ attributeSet , $ attributes , $ options ])) !== 3 ) {
526
532
unset($ configurableProductConfig [$ i ]);
@@ -624,36 +630,6 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
624
630
return $ sku ;
625
631
}
626
632
627
- /**
628
- * @param int $attributes
629
- * @param int $options
630
- * @param string $swatches
631
- * @return array
632
- */
633
- private function getAttributeSet ($ attributes , $ options , $ swatches )
634
- {
635
- $ attributeCode = 'configurable_attribute ' . $ attributes . '_ ' . $ options . '_ ' ;
636
-
637
- return $ this ->attributeSetsFixture ->createAttributeSet (
638
- $ this ->attributePattern ->generateAttributeSet (
639
- $ this ->getAttributeSetName ($ attributes , $ options ),
640
- $ attributes ,
641
- $ options ,
642
- function ($ index , $ attribute ) use ($ attributeCode , $ options , $ swatches ) {
643
- $ data = [
644
- 'attribute_code ' => $ attributeCode . $ index ,
645
- 'frontend_label ' => 'Big Attribute ' . $ attributeCode . $ index ,
646
- ];
647
- return array_replace_recursive (
648
- $ attribute ,
649
- $ data ,
650
- $ this ->swatchesGenerator ->generateSwatchData ($ options , $ attributeCode . $ index , $ swatches )
651
- );
652
- }
653
- )
654
- );
655
- }
656
-
657
633
/**
658
634
* Provide attribute set based on attributes configuration
659
635
*
@@ -662,24 +638,26 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
662
638
*/
663
639
private function getCustomAttributeSet (array $ attributes )
664
640
{
665
- $ attributeSetName = $ this ->getAttributeSetName (
666
- count ($ attributes ),
667
- implode (', ' , array_column ($ attributes , 'options ' ))
668
- );
641
+ $ attributeSetHash = md5 (json_encode ($ attributes ));
642
+ $ attributeSetName = sprintf ('Dynamic Attribute Set %s ' , $ attributeSetHash );
669
643
670
644
$ pattern = $ this ->attributePattern ->generateAttributeSet (
671
645
$ attributeSetName ,
672
646
count ($ attributes ),
673
647
array_column ($ attributes , 'options ' ),
674
- function ($ index , $ attribute ) use ($ attributeSetName , $ attributes ) {
648
+ function ($ index , $ attribute ) use ($ attributeSetName , $ attributes, $ attributeSetHash ) {
675
649
$ swatch = [];
676
- $ attributeCode = substr (uniqid (sprintf ('custom_attribute_%s_ ' , $ index )), 0 , 30 );
650
+ $ attributeCode = substr (
651
+ sprintf ('ca_%s_%s ' , $ index , $ attributeSetHash ),
652
+ 0 ,
653
+ Attribute::ATTRIBUTE_CODE_MAX_LENGTH
654
+ );
677
655
$ data = [
678
656
'attribute_code ' => $ attributeCode ,
679
657
'frontend_label ' => 'Dynamic Attribute ' . $ attributeCode ,
680
658
];
681
659
682
- if (isset ($ attributes [$ index- 1 ]['swatches ' ])) {
660
+ if (isset ($ attributes [$ index - 1 ]['swatches ' ])) {
683
661
$ data ['is_visible_in_advanced_search ' ] = 1 ;
684
662
$ data ['is_searchable ' ] = 1 ;
685
663
$ data ['is_filterable ' ] = 1 ;
@@ -704,18 +682,6 @@ function ($index, $attribute) use ($attributeSetName, $attributes) {
704
682
return $ this ->attributeSetsFixture ->createAttributeSet ($ pattern );
705
683
}
706
684
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 uniqid (sprintf ('Dynamic Attribute Set %s-%s- ' , $ attributesCount , $ optionsCount )) ;
717
- }
718
-
719
685
/**
720
686
* @return array
721
687
*/
@@ -906,7 +872,7 @@ private function convertAttributesToDBFormat(array $attributeSet)
906
872
'attribute_code ' ,
907
873
$ attributeData ['name ' ]
908
874
);
909
- /** @var \Magento\Eav\Model\Entity\ Attribute $attribute */
875
+ /** @var Attribute $attribute */
910
876
foreach ($ attributeCollection as $ attribute ) {
911
877
$ attributeSetId = $ attribute ->getAttributeSetId ();
912
878
$ values = [];
0 commit comments