Skip to content

Commit 1f07d29

Browse files
author
Oleksandr Iegorov
committed
MAGETWO-99927: Product URL rewrites are always regenerated if products are updated via CSV import in default scope
1 parent 8ff967d commit 1f07d29

File tree

1 file changed

+130
-67
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+130
-67
lines changed

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

Lines changed: 130 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @SuppressWarnings(PHPMD.TooManyFields)
3939
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
4040
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
4142
* @since 100.0.2
4243
*/
4344
class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
@@ -1688,73 +1689,27 @@ protected function _saveProducts()
16881689
* Note: to avoid problems with undefined sorting, the value of media gallery items positions
16891690
* must be unique in scope of one product.
16901691
*/
1691-
$position = 0;
1692-
foreach ($rowImages as $column => $columnImages) {
1693-
foreach ($columnImages as $columnImageKey => $columnImage) {
1694-
if (!isset($uploadedImages[$columnImage])) {
1695-
$uploadedFile = $this->uploadMediaFiles($columnImage);
1696-
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
1697-
if ($uploadedFile) {
1698-
$uploadedImages[$columnImage] = $uploadedFile;
1699-
} else {
1700-
unset($rowData[$column]);
1701-
$this->addRowError(
1702-
ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE,
1703-
$rowNum,
1704-
null,
1705-
null,
1706-
ProcessingError::ERROR_LEVEL_NOT_CRITICAL
1707-
);
1708-
}
1709-
} else {
1710-
$uploadedFile = $uploadedImages[$columnImage];
1711-
}
1712-
1713-
if ($uploadedFile && $column !== self::COL_MEDIA_IMAGE) {
1714-
$rowData[$column] = $uploadedFile;
1715-
}
1716-
1717-
if (!$uploadedFile || isset($mediaGallery[$storeId][$rowSku][$uploadedFile])) {
1718-
continue;
1719-
}
1720-
1721-
if (isset($existingImages[$rowSku][$uploadedFile])) {
1722-
$currentFileData = $existingImages[$rowSku][$uploadedFile];
1723-
if (isset($rowLabels[$column][$columnImageKey])
1724-
&& $rowLabels[$column][$columnImageKey] !=
1725-
$currentFileData['label']
1726-
) {
1727-
$labelsForUpdate[] = [
1728-
'label' => $rowLabels[$column][$columnImageKey],
1729-
'imageData' => $currentFileData
1730-
];
1731-
}
1732-
1733-
if (array_key_exists($uploadedFile, $imageHiddenStates)
1734-
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
1735-
) {
1736-
$imagesForChangeVisibility[] = [
1737-
'disabled' => $imageHiddenStates[$uploadedFile],
1738-
'imageData' => $currentFileData
1739-
];
1740-
}
1741-
} else {
1742-
if ($column == self::COL_MEDIA_IMAGE) {
1743-
$rowData[$column][] = $uploadedFile;
1744-
}
1745-
$mediaGallery[$storeId][$rowSku][$uploadedFile] = [
1746-
'attribute_id' => $this->getMediaGalleryAttributeId(),
1747-
'label' => isset($rowLabels[$column][$columnImageKey])
1748-
? $rowLabels[$column][$columnImageKey]
1749-
: '',
1750-
'position' => ++$position,
1751-
'disabled' => isset($imageHiddenStates[$columnImage])
1752-
? $imageHiddenStates[$columnImage] : '0',
1753-
'value' => $uploadedFile,
1754-
];
1755-
}
1756-
}
1757-
}
1692+
list(
1693+
$uploadedImages,
1694+
$rowData,
1695+
$mediaGallery,
1696+
$existingImages,
1697+
$labelsForUpdate,
1698+
$imagesForChangeVisibility
1699+
) = $this->processImages(
1700+
$rowImages,
1701+
$uploadedImages,
1702+
$rowData,
1703+
$rowNum,
1704+
$mediaGallery,
1705+
$storeId,
1706+
$rowSku,
1707+
$existingImages,
1708+
$rowLabels,
1709+
$labelsForUpdate,
1710+
$imageHiddenStates,
1711+
$imagesForChangeVisibility
1712+
);
17581713

17591714
// 6. Attributes phase
17601715
$rowStore = (self::SCOPE_STORE == $rowScope)
@@ -3204,4 +3159,112 @@ private function composeLinkKey(int $productId, int $linkedId, int $linkTypeId)
32043159
{
32053160
return "{$productId}-{$linkedId}-{$linkTypeId}";
32063161
}
3162+
3163+
/**
3164+
* Process images
3165+
*
3166+
* @param array $rowImages
3167+
* @param array $uploadedImages
3168+
* @param array $rowData
3169+
* @param int $rowNum
3170+
* @param array $mediaGallery
3171+
* @param int $storeId
3172+
* @param string $rowSku
3173+
* @param array $existingImages
3174+
* @param array $rowLabels
3175+
* @param array $labelsForUpdate
3176+
* @param array $imageHiddenStates
3177+
* @param array $imagesForChangeVisibility
3178+
* @return array
3179+
*/
3180+
private function processImages(
3181+
array $rowImages,
3182+
array $uploadedImages,
3183+
array $rowData,
3184+
$rowNum,
3185+
array $mediaGallery,
3186+
$storeId,
3187+
$rowSku,
3188+
array $existingImages,
3189+
array $rowLabels,
3190+
array $labelsForUpdate,
3191+
array $imageHiddenStates,
3192+
array $imagesForChangeVisibility
3193+
): array {
3194+
$position = 0;
3195+
foreach ($rowImages as $column => $columnImages) {
3196+
foreach ($columnImages as $columnImageKey => $columnImage) {
3197+
if (!isset($uploadedImages[$columnImage])) {
3198+
$uploadedFile = $this->uploadMediaFiles($columnImage);
3199+
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
3200+
if ($uploadedFile) {
3201+
$uploadedImages[$columnImage] = $uploadedFile;
3202+
} else {
3203+
unset($rowData[$column]);
3204+
$this->addRowError(
3205+
ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE,
3206+
$rowNum,
3207+
null,
3208+
null,
3209+
ProcessingError::ERROR_LEVEL_NOT_CRITICAL
3210+
);
3211+
}
3212+
} else {
3213+
$uploadedFile = $uploadedImages[$columnImage];
3214+
}
3215+
3216+
if ($uploadedFile && $column !== self::COL_MEDIA_IMAGE) {
3217+
$rowData[$column] = $uploadedFile;
3218+
}
3219+
3220+
if (!$uploadedFile || isset($mediaGallery[$storeId][$rowSku][$uploadedFile])) {
3221+
continue;
3222+
}
3223+
3224+
if (isset($existingImages[$rowSku][$uploadedFile])) {
3225+
$currentFileData = $existingImages[$rowSku][$uploadedFile];
3226+
if (isset($rowLabels[$column][$columnImageKey])
3227+
&& $rowLabels[$column][$columnImageKey] !=
3228+
$currentFileData['label']
3229+
) {
3230+
$labelsForUpdate[] = [
3231+
'label' => $rowLabels[$column][$columnImageKey],
3232+
'imageData' => $currentFileData
3233+
];
3234+
}
3235+
3236+
if (array_key_exists($uploadedFile, $imageHiddenStates)
3237+
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
3238+
) {
3239+
$imagesForChangeVisibility[] = [
3240+
'disabled' => $imageHiddenStates[$uploadedFile],
3241+
'imageData' => $currentFileData
3242+
];
3243+
}
3244+
} else {
3245+
if ($column == self::COL_MEDIA_IMAGE) {
3246+
$rowData[$column][] = $uploadedFile;
3247+
}
3248+
$mediaGallery[$storeId][$rowSku][$uploadedFile] = [
3249+
'attribute_id' => $this->getMediaGalleryAttributeId(),
3250+
'label' => isset($rowLabels[$column][$columnImageKey])
3251+
? $rowLabels[$column][$columnImageKey]
3252+
: '',
3253+
'position' => ++$position,
3254+
'disabled' => isset($imageHiddenStates[$columnImage])
3255+
? $imageHiddenStates[$columnImage] : '0',
3256+
'value' => $uploadedFile,
3257+
];
3258+
}
3259+
}
3260+
}
3261+
return [
3262+
$uploadedImages,
3263+
$rowData,
3264+
$mediaGallery,
3265+
$existingImages,
3266+
$labelsForUpdate,
3267+
$imagesForChangeVisibility
3268+
];
3269+
}
32073270
}

0 commit comments

Comments
 (0)