Skip to content

Commit 7ab810b

Browse files
committed
MAGETWO-67240: Duplicate and broken products appear after import
1 parent 6223aa7 commit 7ab810b

File tree

1 file changed

+22
-28
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+22
-28
lines changed

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ protected function _deleteProducts()
925925

926926
foreach ($bunch as $rowNum => $rowData) {
927927
if ($this->validateRow($rowData, $rowNum) && self::SCOPE_DEFAULT == $this->getRowScope($rowData)) {
928-
$idsToDelete[] = $this->getExistSku($rowData[self::COL_SKU])['entity_id'];
928+
$idsToDelete[] = $this->getExistingSku($rowData[self::COL_SKU])['entity_id'];
929929
}
930930
}
931931
if ($idsToDelete) {
@@ -1185,16 +1185,14 @@ protected function _saveLinks()
11851185
: [];
11861186
foreach ($linkSkus as $linkedKey => $linkedSku) {
11871187
$linkedSku = trim($linkedSku);
1188-
if ((!is_null(
1189-
$this->skuProcessor->getNewSku($linkedSku)
1190-
) || $this->isSkuExist($linkedSku)
1191-
) && strtolower($linkedSku) != strtolower($sku)
1188+
if ((!is_null($this->skuProcessor->getNewSku($linkedSku)) || $this->isSkuExist($linkedSku))
1189+
&& strcasecmp($linkedSku, $sku) !== 0
11921190
) {
11931191
$newSku = $this->skuProcessor->getNewSku($linkedSku);
11941192
if (!empty($newSku)) {
11951193
$linkedId = $newSku['entity_id'];
11961194
} else {
1197-
$linkedId = $this->getExistSku($linkedSku)['entity_id'];
1195+
$linkedId = $this->getExistingSku($linkedSku)['entity_id'];
11981196
}
11991197

12001198
if ($linkedId == null) {
@@ -1454,7 +1452,7 @@ protected function getExistingImages($bunch)
14541452
}
14551453

14561454
$this->initMediaGalleryResources();
1457-
$productSKUs = array_map('strtolower', array_column($bunch, self::COL_SKU));
1455+
$productSKUs = array_map('strval', array_column($bunch, self::COL_SKU));
14581456
$select = $this->_connection->select()->from(
14591457
['mg' => $this->mediaGalleryTableName],
14601458
['value' => 'mg.value']
@@ -1536,6 +1534,7 @@ protected function _saveProducts()
15361534
$priceIsGlobal = $this->_catalogData->isPriceGlobal();
15371535
$productLimit = null;
15381536
$productsQty = null;
1537+
$entityLinkField = $this->getProductEntityLinkField();
15391538

15401539
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
15411540
$entityRowsIn = [];
@@ -1605,14 +1604,14 @@ protected function _saveProducts()
16051604
$entityRowsUp[] = [
16061605
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
16071606
'attribute_set_id' => $attributeSetId,
1608-
$this->getProductEntityLinkField() => $this->getExistSku($rowSku)[$this->getProductEntityLinkField()]
1607+
$entityLinkField => $this->getExistingSku($rowSku)[$entityLinkField]
16091608
];
16101609
} else {
16111610
if (!$productLimit || $productsQty < $productLimit) {
16121611
$entityRowsIn[$rowSku] = [
16131612
'attribute_set_id' => $this->skuProcessor->getNewSku($rowSku)['attr_set_id'],
16141613
'type_id' => $this->skuProcessor->getNewSku($rowSku)['type_id'],
1615-
'sku' => $rowData[self::COL_SKU],
1614+
'sku' => $rowSku,
16161615
'has_options' => isset($rowData['has_options']) ? $rowData['has_options'] : 0,
16171616
'created_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
16181617
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
@@ -2382,7 +2381,7 @@ public function validateRow(array $rowData, $rowNum)
23822381
$this->_validatedRows[$rowNum] = true;
23832382

23842383
$rowScope = $this->getRowScope($rowData);
2385-
$sku = strtolower($rowData[self::COL_SKU]);
2384+
$sku = $rowData[self::COL_SKU];
23862385

23872386
// BEHAVIOR_DELETE and BEHAVIOR_REPLACE use specific validation logic
23882387
if (Import::BEHAVIOR_REPLACE == $this->getBehavior()) {
@@ -2421,15 +2420,13 @@ public function validateRow(array $rowData, $rowNum)
24212420
if ($this->isSkuExist($sku) && Import::BEHAVIOR_REPLACE !== $this->getBehavior()) {
24222421
// can we get all necessary data from existent DB product?
24232422
// check for supported type of existing product
2424-
if (isset($this->_productTypeModels[$this->getExistSku($sku)['type_id']])) {
2423+
if (isset($this->_productTypeModels[$this->getExistingSku($sku)['type_id']])) {
24252424
$this->skuProcessor->addNewSku(
24262425
$sku,
24272426
$this->prepareNewSkuData($sku)
24282427
);
24292428
} else {
24302429
$this->addRowError(ValidatorInterface::ERROR_TYPE_UNSUPPORTED, $rowNum);
2431-
// child rows of legacy products with unsupported types are orphans
2432-
$sku = false;
24332430
}
24342431
} else {
24352432
// validate new product type and attribute set
@@ -2454,10 +2451,6 @@ public function validateRow(array $rowData, $rowNum)
24542451
]
24552452
);
24562453
}
2457-
if ($this->getErrorAggregator()->isRowInvalid($rowNum)) {
2458-
// mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
2459-
$sku = false;
2460-
}
24612454
}
24622455

24632456
if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
@@ -2466,16 +2459,13 @@ public function validateRow(array $rowData, $rowNum)
24662459
$rowData[self::COL_ATTR_SET] = $newSku['attr_set_code'];
24672460

24682461
/** @var \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType $productTypeValidator */
2462+
// isRowValid can add error to general errors pull if row is invalid
24692463
$productTypeValidator = $this->_productTypeModels[$newSku['type_id']];
2470-
$rowAttributesValid = $productTypeValidator->isRowValid(
2464+
$productTypeValidator->isRowValid(
24712465
$rowData,
24722466
$rowNum,
24732467
!($this->isSkuExist($sku) && Import::BEHAVIOR_REPLACE !== $this->getBehavior())
24742468
);
2475-
if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) {
2476-
// mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
2477-
$sku = false;
2478-
}
24792469
}
24802470
// validate custom options
24812471
$this->getOptionEntity()->validateRow($rowData, $rowNum);
@@ -2533,11 +2523,11 @@ private function isNeedToValidateUrlKey($rowData)
25332523
private function prepareNewSkuData($sku)
25342524
{
25352525
$data = [];
2536-
foreach ($this->getExistSku($sku) as $key => $value) {
2526+
foreach ($this->getExistingSku($sku) as $key => $value) {
25372527
$data[$key] = $value;
25382528
}
25392529

2540-
$data['attr_set_code'] = $this->_attrSetIdToName[$this->getExistSku($sku)['attr_set_id']];
2530+
$data['attr_set_code'] = $this->_attrSetIdToName[$this->getExistingSku($sku)['attr_set_id']];
25412531

25422532
return $data;
25432533
}
@@ -2930,7 +2920,9 @@ private function parseMultipleValues($labelRow)
29302920
}
29312921

29322922
/**
2933-
* @param $sku
2923+
* Check if product exists for specified SKU
2924+
*
2925+
* @param string $sku
29342926
* @return bool
29352927
*/
29362928
private function isSkuExist($sku)
@@ -2940,10 +2932,12 @@ private function isSkuExist($sku)
29402932
}
29412933

29422934
/**
2943-
* @param $sku
2944-
* @return mixed
2935+
* Get existing product data for specified SKU
2936+
*
2937+
* @param string $sku
2938+
* @return array
29452939
*/
2946-
private function getExistSku($sku)
2940+
private function getExistingSku($sku)
29472941
{
29482942
return $this->_oldSku[strtolower($sku)];
29492943
}

0 commit comments

Comments
 (0)