Skip to content

Commit 4b45119

Browse files
authored
Merge pull request #8369 from magento-performance/ACPT-943
[Performance] REST API Import JSON
2 parents 0b16f58 + 4b88933 commit 4b45119

File tree

19 files changed

+870
-310
lines changed

19 files changed

+870
-310
lines changed

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,26 @@ protected function parseSelections($rowData, $entityId)
181181
return [];
182182
}
183183

184-
$rowData['bundle_values'] = str_replace(
185-
self::BEFORE_OPTION_VALUE_DELIMITER,
186-
$this->_entityModel->getMultipleValueSeparator(),
187-
$rowData['bundle_values']
188-
);
189-
$selections = explode(
190-
Product::PSEUDO_MULTI_LINE_SEPARATOR,
191-
$rowData['bundle_values']
192-
);
184+
if (is_string($rowData['bundle_values'])) {
185+
$rowData['bundle_values'] = str_replace(
186+
self::BEFORE_OPTION_VALUE_DELIMITER,
187+
$this->_entityModel->getMultipleValueSeparator(),
188+
$rowData['bundle_values']
189+
);
190+
$selections = explode(
191+
Product::PSEUDO_MULTI_LINE_SEPARATOR,
192+
$rowData['bundle_values']
193+
);
194+
} else {
195+
$selections = $rowData['bundle_values'];
196+
}
197+
193198
foreach ($selections as $selection) {
194-
$values = explode($this->_entityModel->getMultipleValueSeparator(), $selection);
195-
$option = $this->parseOption($values);
196-
if (isset($option['sku']) && isset($option['name'])) {
197-
if (!isset($this->_cachedOptions[$entityId])) {
198-
$this->_cachedOptions[$entityId] = [];
199-
}
199+
$option = is_string($selection)
200+
? $this->parseOption(explode($this->_entityModel->getMultipleValueSeparator(), $selection))
201+
: $selection;
202+
203+
if (isset($option['sku'], $option['name'])) {
200204
$this->_cachedSkus[] = $option['sku'];
201205
if (!isset($this->_cachedOptions[$entityId][$option['name']])) {
202206
$this->_cachedOptions[$entityId][$option['name']] = [];

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,13 +1558,21 @@ public function getImagesFromRow(array $rowData)
15581558
$labels = [];
15591559
foreach ($this->_imagesArrayKeys as $column) {
15601560
if (!empty($rowData[$column])) {
1561-
$images[$column] = array_unique(
1562-
array_map(
1563-
'trim',
1564-
explode($this->getMultipleValueSeparator(), $rowData[$column])
1565-
)
1566-
);
1567-
1561+
if (is_string($rowData[$column])) {
1562+
$images[$column] = array_unique(
1563+
array_map(
1564+
'trim',
1565+
explode($this->getMultipleValueSeparator(), $rowData[$column])
1566+
)
1567+
);
1568+
} elseif (is_array($rowData[$column])) {
1569+
$images[$column] = array_unique(
1570+
array_map(
1571+
'trim',
1572+
$rowData[$column]
1573+
)
1574+
);
1575+
}
15681576
if (!empty($rowData[$column . '_label'])) {
15691577
$labels[$column] = $this->parseMultipleValues($rowData[$column . '_label']);
15701578

@@ -1765,7 +1773,12 @@ private function saveProductToWebsitePhase(array $rowData) : void
17651773
$this->websitesCache[$rowSku] = [];
17661774
}
17671775
if (!empty($rowData[self::COL_PRODUCT_WEBSITES])) {
1768-
$websiteCodes = explode($this->getMultipleValueSeparator(), $rowData[self::COL_PRODUCT_WEBSITES]);
1776+
$websiteCodes = is_string($rowData[self::COL_PRODUCT_WEBSITES])
1777+
? explode($this->getMultipleValueSeparator(), $rowData[self::COL_PRODUCT_WEBSITES])
1778+
: (is_array($rowData[self::COL_PRODUCT_WEBSITES])
1779+
? $rowData[self::COL_PRODUCT_WEBSITES]
1780+
: []);
1781+
17691782
foreach ($websiteCodes as $websiteCode) {
17701783
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
17711784
$this->websitesCache[$rowSku][$websiteId] = true;
@@ -2159,11 +2172,10 @@ private function getImagesHiddenStates($rowData)
21592172
*/
21602173
protected function processRowCategories($rowData)
21612174
{
2162-
$categoriesString = empty($rowData[self::COL_CATEGORY]) ? '' : $rowData[self::COL_CATEGORY];
21632175
$categoryIds = [];
2164-
if (!empty($categoriesString)) {
2176+
if (!empty($rowData[self::COL_CATEGORY])) {
21652177
$categoryIds = $this->categoryProcessor->upsertCategories(
2166-
$categoriesString,
2178+
$rowData[self::COL_CATEGORY],
21672179
$this->getMultipleValueSeparator()
21682180
);
21692181
foreach ($this->categoryProcessor->getFailedCategories() as $error) {
@@ -2813,7 +2825,13 @@ private function _parseAdditionalAttributes($rowData)
28132825
if (empty($rowData['additional_attributes'])) {
28142826
return $rowData;
28152827
}
2816-
$rowData = array_merge($rowData, $this->getAdditionalAttributes($rowData['additional_attributes']));
2828+
if (is_array($rowData['additional_attributes'])) {
2829+
foreach ($rowData['additional_attributes'] as $key => $value) {
2830+
$rowData[mb_strtolower($key)] = $value;
2831+
}
2832+
} else {
2833+
$rowData = array_merge($rowData, $this->getAdditionalAttributes($rowData['additional_attributes']));
2834+
}
28172835
return $rowData;
28182836
}
28192837

app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,13 +1156,23 @@ protected function _isReadyForSaving(array &$options, array &$titles, array $typ
11561156
*/
11571157
protected function _getMultiRowFormat($rowData)
11581158
{
1159-
// Parse custom options.
1160-
$rowData = $this->_parseCustomOptions($rowData);
1161-
$multiRow = [];
1159+
if (!isset($rowData['custom_options'])) {
1160+
return [];
1161+
}
1162+
1163+
if (is_array($rowData['custom_options'])) {
1164+
$rowData = $this->parseStructuredCustomOptions($rowData);
1165+
} elseif (is_string($rowData['custom_options'])) {
1166+
$rowData = $this->_parseCustomOptions($rowData);
1167+
} else {
1168+
return [];
1169+
}
1170+
11621171
if (empty($rowData['custom_options']) || !is_array($rowData['custom_options'])) {
1163-
return $multiRow;
1172+
return [];
11641173
}
11651174

1175+
$multiRow = [];
11661176
$i = 0;
11671177
foreach ($rowData['custom_options'] as $name => $customOption) {
11681178
$i++;
@@ -1315,11 +1325,12 @@ protected function _importData()
13151325
$multiRowData = $this->_getMultiRowFormat($rowData);
13161326
if (!empty($rowData[self::COLUMN_SKU]) && isset($this->_productsSkuToId[$rowData[self::COLUMN_SKU]])) {
13171327
$this->_rowProductId = $this->_productsSkuToId[$rowData[self::COLUMN_SKU]];
1318-
if (array_key_exists('custom_options', $rowData)
1319-
&& (
1328+
if (array_key_exists('custom_options', $rowData) &&
1329+
(
13201330
$rowData['custom_options'] === null ||
1321-
trim($rowData['custom_options']) === '' ||
1322-
trim($rowData['custom_options']) === $this->_productEntity->getEmptyAttributeValueConstant()
1331+
(is_string($rowData['custom_options']) && trim($rowData['custom_options'])
1332+
=== $this->_productEntity->getEmptyAttributeValueConstant()) ||
1333+
!$rowData['custom_options']
13231334
)
13241335
) {
13251336
$optionsToRemove[] = $this->_rowProductId;
@@ -2084,6 +2095,39 @@ protected function _parseCustomOptions($rowData)
20842095
return $rowData;
20852096
}
20862097

2098+
/**
2099+
* Parse structured custom options to inner format.
2100+
*
2101+
* @param array $rowData
2102+
* @return array
2103+
*/
2104+
private function parseStructuredCustomOptions(array $rowData): array
2105+
{
2106+
if (empty($rowData['custom_options'])) {
2107+
return $rowData;
2108+
}
2109+
2110+
array_walk_recursive($rowData['custom_options'], function (&$value) {
2111+
$value = trim($value);
2112+
});
2113+
2114+
$customOptions = [];
2115+
foreach ($rowData['custom_options'] as $option) {
2116+
$optionName = $option['name'] ?? '';
2117+
if (!isset($customOptions[$optionName])) {
2118+
$customOptions[$optionName] = [];
2119+
}
2120+
if (isset($rowData[Product::COL_STORE_VIEW_CODE])) {
2121+
$option[self::COLUMN_STORE] = $rowData[Product::COL_STORE_VIEW_CODE];
2122+
}
2123+
$customOptions[$optionName][] = $option;
2124+
}
2125+
2126+
$rowData['custom_options'] = $customOptions;
2127+
2128+
return $rowData;
2129+
}
2130+
20872131
/**
20882132
* Clear product sku to id array.
20892133
*

app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogImportExport\Model\Import\Product\Type;
77

88
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
9+
use Magento\CatalogImportExport\Model\Import\Product;
910
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
1011
use Magento\Eav\Model\Entity\Attribute\Source\Table;
1112
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as AttributeOptionCollectionFactory;
@@ -21,6 +22,7 @@
2122
*
2223
* @SuppressWarnings(PHPMD.TooManyFields)
2324
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2426
* @since 100.0.2
2527
*/
2628
abstract class AbstractType
@@ -110,7 +112,7 @@ abstract class AbstractType
110112
/**
111113
* Product entity object.
112114
*
113-
* @var \Magento\CatalogImportExport\Model\Import\Product
115+
* @var Product
114116
*/
115117
protected $_entityModel;
116118

@@ -189,7 +191,7 @@ public function __construct(
189191
if (!isset($params[0])
190192
|| !isset($params[1])
191193
|| !is_object($params[0])
192-
|| !$params[0] instanceof \Magento\CatalogImportExport\Model\Import\Product
194+
|| !$params[0] instanceof Product
193195
) {
194196
throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the parameters.'));
195197
}
@@ -258,7 +260,7 @@ public function retrieveAttribute($attributeCode, $attributeSet)
258260
protected function _getProductAttributes($attrSetData)
259261
{
260262
if (is_array($attrSetData)) {
261-
return $this->_attributes[$attrSetData[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET]];
263+
return $this->_attributes[$attrSetData[Product::COL_ATTR_SET]];
262264
} else {
263265
return $this->_attributes[$attrSetData];
264266
}
@@ -569,23 +571,17 @@ public function isRowValid(array $rowData, $rowNum, $isNewProduct = true)
569571
{
570572
$error = false;
571573
$rowScope = $this->_entityModel->getRowScope($rowData);
572-
if (\Magento\CatalogImportExport\Model\Import\Product::SCOPE_NULL != $rowScope
573-
&& !empty($rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_SKU])
574-
) {
574+
if (Product::SCOPE_NULL != $rowScope && !empty($rowData[Product::COL_SKU])) {
575575
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
576576
// check value for non-empty in the case of required attribute?
577-
if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
577+
if (isset($rowData[$attrCode]) && (!is_array($rowData[$attrCode]) && strlen($rowData[$attrCode]) > 0
578+
|| is_array($rowData[$attrCode]) && !empty($rowData[$attrCode]))) {
578579
$error |= !$this->_entityModel->isAttributeValid($attrCode, $attrParams, $rowData, $rowNum);
579580
} elseif ($this->_isAttributeRequiredCheckNeeded($attrCode) && $attrParams['is_required']) {
580581
// For the default scope - if this is a new product or
581582
// for an old product, if the imported doc has the column present for the attrCode
582-
if (\Magento\CatalogImportExport\Model\Import\Product::SCOPE_DEFAULT == $rowScope &&
583-
($isNewProduct ||
584-
array_key_exists(
585-
$attrCode,
586-
$rowData
587-
))
588-
) {
583+
if (Product::SCOPE_DEFAULT == $rowScope &&
584+
($isNewProduct || array_key_exists($attrCode, $rowData))) {
589585
$this->_entityModel->addRowError(
590586
RowValidatorInterface::ERROR_VALUE_IS_REQUIRED,
591587
$rowNum,
@@ -631,7 +627,8 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
631627
continue;
632628
}
633629
$attrCode = mb_strtolower($attrCode);
634-
if (isset($rowData[$attrCode]) && strlen(trim($rowData[$attrCode]))) {
630+
if (isset($rowData[$attrCode]) && ((is_array($rowData[$attrCode]) && !empty($rowData[$attrCode]))
631+
|| (!is_array($rowData[$attrCode]) && strlen(trim($rowData[$attrCode]))))) {
635632
if (in_array($attrParams['type'], ['select', 'boolean'])) {
636633
$resultAttrs[$attrCode] = $attrParams['options'][strtolower($rowData[$attrCode])];
637634
} elseif ('multiselect' == $attrParams['type']) {

0 commit comments

Comments
 (0)