Skip to content

Commit 6223aa7

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

File tree

1 file changed

+38
-20
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+38
-20
lines changed

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

Lines changed: 38 additions & 20 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->_oldSku[strtolower($rowData[self::COL_SKU])]['entity_id'];
928+
$idsToDelete[] = $this->getExistSku($rowData[self::COL_SKU])['entity_id'];
929929
}
930930
}
931931
if ($idsToDelete) {
@@ -1115,7 +1115,7 @@ protected function _prepareRowForDb(array $rowData)
11151115

11161116
$lastSku = $rowData[self::COL_SKU];
11171117

1118-
if (isset($this->_oldSku[strtolower($lastSku)])) {
1118+
if ($this->isSkuExist($lastSku)) {
11191119
$newSku = $this->skuProcessor->getNewSku($lastSku);
11201120
$rowData[self::COL_ATTR_SET] = $newSku['attr_set_code'];
11211121
$rowData[self::COL_TYPE] = $newSku['type_id'];
@@ -1187,15 +1187,14 @@ protected function _saveLinks()
11871187
$linkedSku = trim($linkedSku);
11881188
if ((!is_null(
11891189
$this->skuProcessor->getNewSku($linkedSku)
1190-
) || isset(
1191-
$this->_oldSku[$linkedSku]
1192-
)) && $linkedSku != $sku
1190+
) || $this->isSkuExist($linkedSku)
1191+
) && strtolower($linkedSku) != strtolower($sku)
11931192
) {
11941193
$newSku = $this->skuProcessor->getNewSku($linkedSku);
11951194
if (!empty($newSku)) {
11961195
$linkedId = $newSku['entity_id'];
11971196
} else {
1198-
$linkedId = $this->_oldSku[strtolower($linkedSku)]['entity_id'];
1197+
$linkedId = $this->getExistSku($linkedSku)['entity_id'];
11991198
}
12001199

12011200
if ($linkedId == null) {
@@ -1582,7 +1581,7 @@ protected function _saveProducts()
15821581
}
15831582

15841583
// 1. Entity phase
1585-
if (isset($this->_oldSku[strtolower($rowSku)])) {
1584+
if ($this->isSkuExist($rowSku)) {
15861585
// existing row
15871586
if (isset($rowData['attribute_set_code'])) {
15881587
$attributeSetId = $this->catalogConfig->getAttributeSetId(
@@ -1606,7 +1605,7 @@ protected function _saveProducts()
16061605
$entityRowsUp[] = [
16071606
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
16081607
'attribute_set_id' => $attributeSetId,
1609-
$this->getProductEntityLinkField() => $this->_oldSku[strtolower($rowSku)][$this->getProductEntityLinkField()]
1608+
$this->getProductEntityLinkField() => $this->getExistSku($rowSku)[$this->getProductEntityLinkField()]
16101609
];
16111610
} else {
16121611
if (!$productLimit || $productsQty < $productLimit) {
@@ -1770,7 +1769,7 @@ protected function _saveProducts()
17701769

17711770
$rowData = $productTypeModel->prepareAttributesWithDefaultValueForSave(
17721771
$rowData,
1773-
!isset($this->_oldSku[$rowSku])
1772+
!$this->isSkuExist($rowSku)
17741773
);
17751774
$product = $this->_proxyProdFactory->create(['data' => $rowData]);
17761775

@@ -1812,7 +1811,7 @@ protected function _saveProducts()
18121811
} elseif (self::SCOPE_STORE == $attribute->getIsGlobal()) {
18131812
$storeIds = [$rowStore];
18141813
}
1815-
if (!isset($this->_oldSku[$rowSku])) {
1814+
if (!$this->isSkuExist($rowSku)) {
18161815
$storeIds[] = 0;
18171816
}
18181817
}
@@ -2192,7 +2191,7 @@ protected function _saveStockItem()
21922191
}
21932192

21942193
$row = [];
2195-
$sku = strtolower($rowData[self::COL_SKU]);
2194+
$sku = $rowData[self::COL_SKU];
21962195
$row['product_id'] = $this->skuProcessor->getNewSku($sku)['entity_id'];
21972196
$productIdsToReindex[] = $row['product_id'];
21982197

@@ -2387,13 +2386,13 @@ public function validateRow(array $rowData, $rowNum)
23872386

23882387
// BEHAVIOR_DELETE and BEHAVIOR_REPLACE use specific validation logic
23892388
if (Import::BEHAVIOR_REPLACE == $this->getBehavior()) {
2390-
if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$sku])) {
2389+
if (self::SCOPE_DEFAULT == $rowScope && !$this->isSkuExist($sku)) {
23912390
$this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum);
23922391
return false;
23932392
}
23942393
}
23952394
if (Import::BEHAVIOR_DELETE == $this->getBehavior()) {
2396-
if (self::SCOPE_DEFAULT == $rowScope && !isset($this->_oldSku[$sku])) {
2395+
if (self::SCOPE_DEFAULT == $rowScope && !$this->isSkuExist($sku)) {
23972396
$this->addRowError(ValidatorInterface::ERROR_SKU_NOT_FOUND_FOR_DELETE, $rowNum);
23982397
return false;
23992398
}
@@ -2406,9 +2405,9 @@ public function validateRow(array $rowData, $rowNum)
24062405
}
24072406
}
24082407

2409-
if (null === $rowData[self::COL_SKU]) {
2408+
if (null === $sku) {
24102409
$this->addRowError(ValidatorInterface::ERROR_SKU_IS_EMPTY, $rowNum);
2411-
} elseif (false === $rowData[self::COL_SKU]) {
2410+
} elseif (false === $sku) {
24122411
$this->addRowError(ValidatorInterface::ERROR_ROW_IS_ORPHAN, $rowNum);
24132412
} elseif (self::SCOPE_STORE == $rowScope
24142413
&& !$this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])
@@ -2419,10 +2418,10 @@ public function validateRow(array $rowData, $rowNum)
24192418
// SKU is specified, row is SCOPE_DEFAULT, new product block begins
24202419
$this->_processedEntitiesCount++;
24212420

2422-
if (isset($this->_oldSku[$sku]) && Import::BEHAVIOR_REPLACE !== $this->getBehavior()) {
2421+
if ($this->isSkuExist($sku) && Import::BEHAVIOR_REPLACE !== $this->getBehavior()) {
24232422
// can we get all necessary data from existent DB product?
24242423
// check for supported type of existing product
2425-
if (isset($this->_productTypeModels[$this->_oldSku[$sku]['type_id']])) {
2424+
if (isset($this->_productTypeModels[$this->getExistSku($sku)['type_id']])) {
24262425
$this->skuProcessor->addNewSku(
24272426
$sku,
24282427
$this->prepareNewSkuData($sku)
@@ -2471,7 +2470,7 @@ public function validateRow(array $rowData, $rowNum)
24712470
$rowAttributesValid = $productTypeValidator->isRowValid(
24722471
$rowData,
24732472
$rowNum,
2474-
!(isset($this->_oldSku[$sku]) && Import::BEHAVIOR_REPLACE !== $this->getBehavior())
2473+
!($this->isSkuExist($sku) && Import::BEHAVIOR_REPLACE !== $this->getBehavior())
24752474
);
24762475
if (!$rowAttributesValid && self::SCOPE_DEFAULT == $rowScope) {
24772476
// mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
@@ -2534,11 +2533,11 @@ private function isNeedToValidateUrlKey($rowData)
25342533
private function prepareNewSkuData($sku)
25352534
{
25362535
$data = [];
2537-
foreach ($this->_oldSku[$sku] as $key => $value) {
2536+
foreach ($this->getExistSku($sku) as $key => $value) {
25382537
$data[$key] = $value;
25392538
}
25402539

2541-
$data['attr_set_code'] = $this->_attrSetIdToName[$this->_oldSku[$sku]['attr_set_id']];
2540+
$data['attr_set_code'] = $this->_attrSetIdToName[$this->getExistSku($sku)['attr_set_id']];
25422541

25432542
return $data;
25442543
}
@@ -2929,4 +2928,23 @@ private function parseMultipleValues($labelRow)
29292928
$this->getMultipleValueSeparator()
29302929
);
29312930
}
2931+
2932+
/**
2933+
* @param $sku
2934+
* @return bool
2935+
*/
2936+
private function isSkuExist($sku)
2937+
{
2938+
$sku = strtolower($sku);
2939+
return isset($this->_oldSku[$sku]);
2940+
}
2941+
2942+
/**
2943+
* @param $sku
2944+
* @return mixed
2945+
*/
2946+
private function getExistSku($sku)
2947+
{
2948+
return $this->_oldSku[strtolower($sku)];
2949+
}
29322950
}

0 commit comments

Comments
 (0)