Skip to content

Commit 9ac7cf9

Browse files
committed
ACPT-1465: Import configurable product
1 parent 5ee7e52 commit 9ac7cf9

File tree

1 file changed

+90
-55
lines changed
  • app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type

1 file changed

+90
-55
lines changed

app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php

Lines changed: 90 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -547,61 +547,28 @@ protected function _processSuperData()
547547
protected function _parseVariations($rowData)
548548
{
549549
$additionalRows = [];
550+
550551
if (empty($rowData['configurable_variations'])) {
551552
return $additionalRows;
552-
} elseif (!empty($rowData['store_view_code'])) {
553+
}
554+
555+
if (!empty($rowData['store_view_code'])) {
553556
throw new LocalizedException(
554557
__(
555558
'Product with assigned super attributes should not have specified "%1" value',
556559
'store_view_code'
557560
)
558561
);
559562
}
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+
561568
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);
589570

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'])) {
605572
throw new LocalizedException(
606573
__(
607574
sprintf(
@@ -611,11 +578,73 @@ protected function _parseVariations($rowData)
611578
)
612579
);
613580
}
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+
}
614597
}
615598

616599
return $additionalRows;
617600
}
618601

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+
619648
/**
620649
* Parse variation labels to array
621650
* ...attribute_code => label ...
@@ -631,21 +660,27 @@ protected function _parseVariationLabels($rowData)
631660
if (!isset($rowData['configurable_variation_labels'])) {
632661
return $labels;
633662
}
634-
$pairFieldAndValue = explode(
635-
$this->_entityModel->getMultipleValueSeparator(),
636-
$rowData['configurable_variation_labels']
637-
);
638663

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+
}
646676
}
647677
}
678+
} else {
679+
foreach ($variationLabels as $attrCode => $value) {
680+
$labels[trim($attrCode)] = trim($value);
681+
}
648682
}
683+
649684
return $labels;
650685
}
651686

0 commit comments

Comments
 (0)