Skip to content

Commit 9174be3

Browse files
committed
MAGETWO-91594: Unable to finish import when one product divided between import "bunches"
- Changing importData behavior for options and option titles
1 parent 3e3c5f1 commit 9174be3

File tree

1 file changed

+77
-7
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import/Product

1 file changed

+77
-7
lines changed

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

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ class Option extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
333333
*/
334334
private $optionTypeTitles;
335335

336+
/**
337+
* @var array
338+
*/
339+
private $lastOptionTitle;
340+
336341
/**
337342
* @param \Magento\ImportExport\Model\ResourceModel\Import\Data $importData
338343
* @param ResourceConnection $resource
@@ -1206,7 +1211,6 @@ private function addFileOptions($result, $optionRow)
12061211
protected function _importData()
12071212
{
12081213
$this->_initProductsSku();
1209-
12101214
$nextOptionId = $this->_resourceHelper->getNextAutoincrement($this->_tables['catalog_product_option']);
12111215
$nextValueId = $this->_resourceHelper->getNextAutoincrement(
12121216
$this->_tables['catalog_product_option_type_value']
@@ -1225,7 +1229,6 @@ protected function _importData()
12251229
$parentCount = [];
12261230
$childCount = [];
12271231
$optionsToRemove = [];
1228-
12291232
foreach ($bunch as $rowNumber => $rowData) {
12301233
if (isset($optionId, $valueId) && empty($rowData[PRODUCT::COL_STORE_VIEW_CODE])) {
12311234
$nextOptionId = $optionId;
@@ -1273,17 +1276,26 @@ protected function _importData()
12731276
$parentCount,
12741277
$childCount
12751278
);
1279+
12761280
$this->_collectOptionTitle($combinedData, $prevOptionId, $titles);
1281+
$this->checkOptionTitles(
1282+
$options,
1283+
$titles,
1284+
$combinedData,
1285+
$prevOptionId,
1286+
$optionId,
1287+
$products,
1288+
$prices
1289+
);
12771290
}
12781291
}
1279-
12801292
$this->removeExistingOptions($products, $optionsToRemove);
1281-
12821293
$types = [
12831294
'values' => $typeValues,
12841295
'prices' => $typePrices,
12851296
'titles' => $typeTitles,
12861297
];
1298+
$this->setLastOptionTitle($titles);
12871299
//Save prepared custom options data.
12881300
$this->savePreparedCustomOptions(
12891301
$products,
@@ -1293,10 +1305,64 @@ protected function _importData()
12931305
$types
12941306
);
12951307
}
1296-
12971308
return true;
12981309
}
12991310

1311+
/**
1312+
* If products were split up between bunches,
1313+
* this function will add needed option for option titles
1314+
*
1315+
* @param array $options
1316+
* @param array $titles
1317+
* @param array $combinedData
1318+
* @param int $prevOptionId
1319+
* @param int $optionId
1320+
* @param array $products
1321+
* @param array $prices
1322+
* @return void
1323+
*/
1324+
private function checkOptionTitles(
1325+
array &$options,
1326+
array &$titles,
1327+
array $combinedData,
1328+
int &$prevOptionId,
1329+
int &$optionId,
1330+
array $products,
1331+
array $prices
1332+
) : void {
1333+
$titlesCount = count($titles);
1334+
if ($titlesCount > 0 && count($options) !== $titlesCount) {
1335+
$combinedData[Product::COL_STORE_VIEW_CODE] = '';
1336+
$optionId--;
1337+
$option = $this->_collectOptionMainData(
1338+
$combinedData,
1339+
$prevOptionId,
1340+
$optionId,
1341+
$products,
1342+
$prices
1343+
);
1344+
if ($option) {
1345+
$options[] = $option;
1346+
}
1347+
}
1348+
}
1349+
1350+
/**
1351+
* Setting last Custom Option Title
1352+
* to use it later in _collectOptionTitle
1353+
* to set correct title for default store view
1354+
*
1355+
* @param array $titles
1356+
*/
1357+
private function setLastOptionTitle(array &$titles) : void
1358+
{
1359+
if (count($titles) > 0) {
1360+
end($titles);
1361+
$key = key($titles);
1362+
$this->lastOptionTitle[$key] = $titles[$key];
1363+
}
1364+
}
1365+
13001366
/**
13011367
* Remove all existing options if import behaviour is APPEND
13021368
* in other case remove options for products with empty "custom_options" row only.
@@ -1447,8 +1513,12 @@ protected function _collectOptionTitle(array $rowData, $prevOptionId, array &$ti
14471513
$defaultStoreId = Store::DEFAULT_STORE_ID;
14481514
if (!empty($rowData[self::COLUMN_TITLE])) {
14491515
if (!isset($titles[$prevOptionId][$defaultStoreId])) {
1450-
// ensure default title is set
1451-
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
1516+
if (isset($this->lastOptionTitle[$prevOptionId])) {
1517+
$titles[$prevOptionId] = $this->lastOptionTitle[$prevOptionId];
1518+
unset($this->lastOptionTitle);
1519+
} else {
1520+
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
1521+
}
14521522
}
14531523
$titles[$prevOptionId][$this->_rowStoreId] = $rowData[self::COLUMN_TITLE];
14541524
}

0 commit comments

Comments
 (0)