@@ -547,61 +547,28 @@ protected function _processSuperData()
547
547
protected function _parseVariations ($ rowData )
548
548
{
549
549
$ additionalRows = [];
550
+
550
551
if (empty ($ rowData ['configurable_variations ' ])) {
551
552
return $ additionalRows ;
552
- } elseif (!empty ($ rowData ['store_view_code ' ])) {
553
+ }
554
+
555
+ if (!empty ($ rowData ['store_view_code ' ])) {
553
556
throw new LocalizedException (
554
557
__ (
555
558
'Product with assigned super attributes should not have specified "%1" value ' ,
556
559
'store_view_code '
557
560
)
558
561
);
559
562
}
560
- $ variations = explode (ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR , $ rowData ['configurable_variations ' ]);
563
+
564
+ $ variations = is_array ($ rowData ['configurable_variations ' ])
565
+ ? $ rowData ['configurable_variations ' ]
566
+ : explode (ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR , $ rowData ['configurable_variations ' ]);
567
+
561
568
foreach ($ variations as $ variation ) {
562
- $ fieldAndValuePairsText = explode ($ this ->_entityModel ->getMultipleValueSeparator (), $ variation );
563
- $ additionalRow = [];
564
-
565
- $ fieldAndValuePairs = [];
566
- foreach ($ fieldAndValuePairsText as $ nameAndValue ) {
567
- // If field value contains comma. For example: sku=C100-10,2cm,size=10,2cm
568
- // then this results in $fieldAndValuePairsText = ["sku=C100-10", "2cm", "size=10", "2cm"]
569
- // This code block makes sure that the array element that do not contain the equal sign "="
570
- // will be appended to the preceding element value.
571
- // As a result $fieldAndValuePairs = ["sku" => "C100-10,2cm", "size" => "10,2cm"]
572
- if (strpos ($ nameAndValue , ImportProduct::PAIR_NAME_VALUE_SEPARATOR ) === false
573
- && isset ($ fieldName )
574
- && isset ($ fieldAndValuePairs [$ fieldName ])
575
- ) {
576
- $ fieldAndValuePairs [$ fieldName ] .= $ this ->_entityModel ->getMultipleValueSeparator () . $ nameAndValue ;
577
- continue ;
578
- }
579
- $ nameAndValue = explode (ImportProduct::PAIR_NAME_VALUE_SEPARATOR , $ nameAndValue , 2 );
580
- if ($ nameAndValue ) {
581
- $ value = isset ($ nameAndValue [1 ]) ? trim ($ nameAndValue [1 ]) : '' ;
582
- // Ignoring field names' case.
583
- $ fieldName = isset ($ nameAndValue [0 ]) ? strtolower (trim ($ nameAndValue [0 ])) : '' ;
584
- if ($ fieldName ) {
585
- $ fieldAndValuePairs [$ fieldName ] = $ value ;
586
- }
587
- }
588
- }
569
+ $ fieldAndValuePairs = $ this ->getFieldAndValuePairs ($ variation );
589
570
590
- if (!empty ($ fieldAndValuePairs ['sku ' ])) {
591
- $ position = 0 ;
592
- $ additionalRow ['_super_products_sku ' ] = strtolower ($ fieldAndValuePairs ['sku ' ]);
593
- unset($ fieldAndValuePairs ['sku ' ]);
594
- $ additionalRow ['display ' ] = $ fieldAndValuePairs ['display ' ] ?? 1 ;
595
- unset($ fieldAndValuePairs ['display ' ]);
596
- foreach ($ fieldAndValuePairs as $ attrCode => $ attrValue ) {
597
- $ additionalRow ['_super_attribute_code ' ] = $ attrCode ;
598
- $ additionalRow ['_super_attribute_option ' ] = $ attrValue ;
599
- $ additionalRow ['_super_attribute_position ' ] = $ position ;
600
- $ additionalRows [] = $ additionalRow ;
601
- $ additionalRow = [];
602
- $ position ++;
603
- }
604
- } else {
571
+ if (empty ($ fieldAndValuePairs ['sku ' ])) {
605
572
throw new LocalizedException (
606
573
__ (
607
574
sprintf (
@@ -611,11 +578,73 @@ protected function _parseVariations($rowData)
611
578
)
612
579
);
613
580
}
581
+
582
+ $ additionalRow = [
583
+ '_super_products_sku ' => strtolower ($ fieldAndValuePairs ['sku ' ]),
584
+ 'display ' => $ fieldAndValuePairs ['display ' ] ?? 1 ,
585
+ ];
586
+ unset($ fieldAndValuePairs ['sku ' ], $ fieldAndValuePairs ['display ' ]);
587
+
588
+ $ position = 0 ;
589
+ foreach ($ fieldAndValuePairs as $ attrCode => $ attrValue ) {
590
+ $ additionalRow ['_super_attribute_code ' ] = $ attrCode ;
591
+ $ additionalRow ['_super_attribute_option ' ] = $ attrValue ;
592
+ $ additionalRow ['_super_attribute_position ' ] = $ position ;
593
+ $ additionalRows [] = $ additionalRow ;
594
+ $ additionalRow = [];
595
+ $ position ++;
596
+ }
614
597
}
615
598
616
599
return $ additionalRows ;
617
600
}
618
601
602
+ /**
603
+ * Get field and value pairs.
604
+ *
605
+ * @param array|string $variation
606
+ * @return array
607
+ */
608
+ private function getFieldAndValuePairs (array |string $ variation ): array
609
+ {
610
+ if (is_array ($ variation )) {
611
+ return $ variation ;
612
+ }
613
+
614
+ $ fieldAndValuePairsText = explode ($ this ->_entityModel ->getMultipleValueSeparator (), $ variation );
615
+ $ fieldAndValuePairs = [];
616
+ $ fieldName = null ;
617
+
618
+ foreach ($ fieldAndValuePairsText as $ nameAndValue ) {
619
+ // If field value contains comma. For example: sku=C100-10,2cm,size=10,2cm
620
+ // then this results in $fieldAndValuePairsText = ["sku=C100-10", "2cm", "size=10", "2cm"]
621
+ // This code block makes sure that the array element that do not contain the equal sign "="
622
+ // will be appended to the preceding element value.
623
+ // As a result $fieldAndValuePairs = ["sku" => "C100-10,2cm", "size" => "10,2cm"]
624
+ if (!str_contains ($ nameAndValue , ImportProduct::PAIR_NAME_VALUE_SEPARATOR )
625
+ && isset ($ fieldName )
626
+ && isset ($ fieldAndValuePairs [$ fieldName ])
627
+ ) {
628
+ $ fieldAndValuePairs [$ fieldName ] .= $ this ->_entityModel ->getMultipleValueSeparator () . $ nameAndValue ;
629
+ continue ;
630
+ }
631
+
632
+ $ nameAndValue = explode (ImportProduct::PAIR_NAME_VALUE_SEPARATOR , $ nameAndValue , 2 );
633
+
634
+ if ($ nameAndValue ) {
635
+ $ value = isset ($ nameAndValue [1 ]) ? trim ($ nameAndValue [1 ]) : '' ;
636
+ // Ignoring field names' case.
637
+ $ fieldName = isset ($ nameAndValue [0 ]) ? strtolower (trim ($ nameAndValue [0 ])) : '' ;
638
+
639
+ if ($ fieldName ) {
640
+ $ fieldAndValuePairs [$ fieldName ] = $ value ;
641
+ }
642
+ }
643
+ }
644
+
645
+ return $ fieldAndValuePairs ;
646
+ }
647
+
619
648
/**
620
649
* Parse variation labels to array
621
650
* ...attribute_code => label ...
@@ -631,21 +660,27 @@ protected function _parseVariationLabels($rowData)
631
660
if (!isset ($ rowData ['configurable_variation_labels ' ])) {
632
661
return $ labels ;
633
662
}
634
- $ pairFieldAndValue = explode (
635
- $ this ->_entityModel ->getMultipleValueSeparator (),
636
- $ rowData ['configurable_variation_labels ' ]
637
- );
638
663
639
- foreach ($ pairFieldAndValue as $ nameAndValue ) {
640
- $ nameAndValue = explode (ImportProduct::PAIR_NAME_VALUE_SEPARATOR , $ nameAndValue );
641
- if ($ nameAndValue ) {
642
- $ value = isset ($ nameAndValue [1 ]) ? trim ($ nameAndValue [1 ]) : '' ;
643
- $ attrCode = isset ($ nameAndValue [0 ]) ? trim ($ nameAndValue [0 ]) : '' ;
644
- if ($ attrCode ) {
645
- $ labels [$ attrCode ] = $ value ;
664
+ $ variationLabels = $ rowData ['configurable_variation_labels ' ];
665
+ if (!is_array ($ variationLabels )) {
666
+ $ pairFieldAndValue = explode ($ this ->_entityModel ->getMultipleValueSeparator (), $ variationLabels );
667
+
668
+ foreach ($ pairFieldAndValue as $ nameAndValue ) {
669
+ $ nameAndValue = explode (ImportProduct::PAIR_NAME_VALUE_SEPARATOR , $ nameAndValue , 2 );
670
+ if ($ nameAndValue ) {
671
+ $ value = isset ($ nameAndValue [1 ]) ? trim ($ nameAndValue [1 ]) : '' ;
672
+ $ attrCode = isset ($ nameAndValue [0 ]) ? trim ($ nameAndValue [0 ]) : '' ;
673
+ if ($ attrCode ) {
674
+ $ labels [$ attrCode ] = $ value ;
675
+ }
646
676
}
647
677
}
678
+ } else {
679
+ foreach ($ variationLabels as $ attrCode => $ value ) {
680
+ $ labels [trim ($ attrCode )] = trim ($ value );
681
+ }
648
682
}
683
+
649
684
return $ labels ;
650
685
}
651
686
0 commit comments