Skip to content

Commit e7d6486

Browse files
author
Alexey Yakimovich
committed
MAGETWO-92226: Product export creates 2 rows for Simple product with custom options
- Fixed an issue with incorrect product export process with custom options;
1 parent b8892f0 commit e7d6486

File tree

1 file changed

+65
-23
lines changed
  • app/code/Magento/CatalogImportExport/Model/Export

1 file changed

+65
-23
lines changed

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

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,11 +1290,23 @@ private function appendMultirowData(&$dataRow, $multiRawData)
12901290
}
12911291

12921292
if (!empty($multiRawData['customOptionsData'][$productLinkId][$storeId])) {
1293+
$shouldBeMerged = true;
12931294
$customOptionsRows = $multiRawData['customOptionsData'][$productLinkId][$storeId];
1294-
$multiRawData['customOptionsData'][$productLinkId][$storeId] = [];
1295-
$customOptions = implode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $customOptionsRows);
12961295

1297-
$dataRow = array_merge($dataRow, ['custom_options' => $customOptions]);
1296+
if ($storeId != Store::DEFAULT_STORE_ID
1297+
&& !empty($multiRawData['customOptionsData'][$productLinkId][Store::DEFAULT_STORE_ID])
1298+
) {
1299+
$defaultCustomOptions = $multiRawData['customOptionsData'][$productLinkId][Store::DEFAULT_STORE_ID];
1300+
if (!array_diff($defaultCustomOptions, $customOptionsRows)) {
1301+
$shouldBeMerged = false;
1302+
}
1303+
}
1304+
1305+
if ($shouldBeMerged) {
1306+
$multiRawData['customOptionsData'][$productLinkId][$storeId] = [];
1307+
$customOptions = implode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $customOptionsRows);
1308+
$dataRow = array_merge($dataRow, ['custom_options' => $customOptions]);
1309+
}
12981310
}
12991311

13001312
if (empty($dataRow)) {
@@ -1390,6 +1402,7 @@ protected function optionRowToCellString($option)
13901402
protected function getCustomOptionsData($productIds)
13911403
{
13921404
$customOptionsData = [];
1405+
$defaultOptionsData = [];
13931406

13941407
foreach (array_keys($this->_storeIdToCode) as $storeId) {
13951408
$options = $this->_optionColFactory->create();
@@ -1402,38 +1415,42 @@ protected function getCustomOptionsData($productIds)
14021415
->addValuesToResult($storeId);
14031416

14041417
foreach ($options as $option) {
1418+
$optionData = $option->toArray();
14051419
$row = [];
14061420
$productId = $option['product_id'];
14071421
$row['name'] = $option['title'];
14081422
$row['type'] = $option['type'];
1409-
if (Store::DEFAULT_STORE_ID === $storeId) {
1410-
$row['required'] = $option['is_require'];
1411-
$row['price'] = $option['price'];
1412-
$row['price_type'] = ($option['price_type'] === 'percent') ? 'percent' : 'fixed';
1413-
$row['sku'] = $option['sku'];
1414-
if ($option['max_characters']) {
1415-
$row['max_characters'] = $option['max_characters'];
1416-
}
1417-
1418-
foreach (['file_extension', 'image_size_x', 'image_size_y'] as $fileOptionKey) {
1419-
if (!isset($option[$fileOptionKey])) {
1420-
continue;
1421-
}
14221423

1423-
$row[$fileOptionKey] = $option[$fileOptionKey];
1424+
$row['required'] = $this->getOptionValue('is_require', $defaultOptionsData, $optionData);
1425+
$row['price'] = $this->getOptionValue('price', $defaultOptionsData, $optionData);
1426+
$row['sku'] = $this->getOptionValue('sku', $defaultOptionsData, $optionData);
1427+
if (array_key_exists('max_characters', $optionData)
1428+
|| array_key_exists('max_characters', $defaultOptionsData)
1429+
) {
1430+
$row['max_characters'] = $this->getOptionValue('max_characters', $defaultOptionsData, $optionData);
1431+
}
1432+
foreach (['file_extension', 'image_size_x', 'image_size_y'] as $fileOptionKey) {
1433+
if (isset($option[$fileOptionKey]) || isset($defaultOptionsData[$fileOptionKey])) {
1434+
$row[$fileOptionKey] = $this->getOptionValue($fileOptionKey, $defaultOptionsData, $optionData);
14241435
}
14251436
}
1437+
$percentType = $this->getOptionValue('price_type', $defaultOptionsData, $optionData);
1438+
$row['price_type'] = ($percentType === 'percent') ? 'percent' : 'fixed';
1439+
1440+
if (Store::DEFAULT_STORE_ID === $storeId) {
1441+
$optionId = $option['option_id'];
1442+
$defaultOptionsData[$optionId] = $option->toArray();
1443+
}
1444+
14261445
$values = $option->getValues();
14271446

14281447
if ($values) {
14291448
foreach ($values as $value) {
14301449
$row['option_title'] = $value['title'];
1431-
if (Store::DEFAULT_STORE_ID === $storeId) {
1432-
$row['option_title'] = $value['title'];
1433-
$row['price'] = $value['price'];
1434-
$row['price_type'] = ($value['price_type'] === 'percent') ? 'percent' : 'fixed';
1435-
$row['sku'] = $value['sku'];
1436-
}
1450+
$row['option_title'] = $value['title'];
1451+
$row['price'] = $value['price'];
1452+
$row['price_type'] = ($value['price_type'] === 'percent') ? 'percent' : 'fixed';
1453+
$row['sku'] = $value['sku'];
14371454
$customOptionsData[$productId][$storeId][] = $this->optionRowToCellString($row);
14381455
}
14391456
} else {
@@ -1447,6 +1464,31 @@ protected function getCustomOptionsData($productIds)
14471464
return $customOptionsData;
14481465
}
14491466

1467+
/**
1468+
* Get value for custom option according to store or default value
1469+
*
1470+
* @param string $optionName
1471+
* @param array $defaultOptionsData
1472+
* @param array $optionData
1473+
* @return mixed
1474+
*/
1475+
private function getOptionValue($optionName, $defaultOptionsData, $optionData)
1476+
{
1477+
$optionId = $optionData['option_id'];
1478+
1479+
if (array_key_exists($optionName, $optionData) && $optionData[$optionName] !== null) {
1480+
return $optionData[$optionName];
1481+
}
1482+
1483+
if (array_key_exists($optionId, $defaultOptionsData)
1484+
&& array_key_exists($optionName, $defaultOptionsData[$optionId])
1485+
) {
1486+
return $defaultOptionsData[$optionId][$optionName];
1487+
}
1488+
1489+
return null;
1490+
}
1491+
14501492
/**
14511493
* Clean up already loaded attribute collection.
14521494
*

0 commit comments

Comments
 (0)