Skip to content

Commit 5d3c733

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-94432' into 2.2-develop-pr47
2 parents fd5966d + 99a8a0a commit 5d3c733

File tree

2 files changed

+105
-20
lines changed

2 files changed

+105
-20
lines changed

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

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ protected function _saveProducts()
15431543
$tierPrices = [];
15441544
$mediaGallery = [];
15451545
$labelsForUpdate = [];
1546+
$imagesForChangeVisibility = [];
15461547
$uploadedImages = [];
15471548
$previousType = null;
15481549
$prevAttributeSet = null;
@@ -1662,21 +1663,24 @@ protected function _saveProducts()
16621663
}
16631664

16641665
// 5. Media gallery phase
1665-
$disabledImages = [];
16661666
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
16671667
$storeId = !empty($rowData[self::COL_STORE])
16681668
? $this->getStoreIdByCode($rowData[self::COL_STORE])
16691669
: Store::DEFAULT_STORE_ID;
1670-
if (isset($rowData['_media_is_disabled']) && strlen(trim($rowData['_media_is_disabled']))) {
1671-
$disabledImages = array_flip(
1672-
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
1673-
);
1670+
$imageHiddenStates = $this->getImagesHiddenStates($rowData);
1671+
foreach (array_keys($imageHiddenStates) as $image) {
1672+
if (array_key_exists($rowSku, $existingImages)
1673+
&& array_key_exists($image, $existingImages[$rowSku])
1674+
) {
1675+
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
1676+
$uploadedImages[$image] = $image;
1677+
}
1678+
16741679
if (empty($rowImages)) {
1675-
foreach (array_keys($disabledImages) as $disabledImage) {
1676-
$rowImages[self::COL_MEDIA_IMAGE][] = $disabledImage;
1677-
}
1680+
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
16781681
}
16791682
}
1683+
16801684
$rowData[self::COL_MEDIA_IMAGE] = [];
16811685

16821686
/*
@@ -1710,12 +1714,22 @@ protected function _saveProducts()
17101714

17111715
if ($uploadedFile && !isset($mediaGallery[$storeId][$rowSku][$uploadedFile])) {
17121716
if (isset($existingImages[$rowSku][$uploadedFile])) {
1717+
$currentFileData = $existingImages[$rowSku][$uploadedFile];
17131718
if (isset($rowLabels[$column][$columnImageKey])
1714-
&& $rowLabels[$column][$columnImageKey] != $existingImages[$rowSku][$uploadedFile]['label']
1719+
&& $rowLabels[$column][$columnImageKey] != $currentFileData['label']
17151720
) {
17161721
$labelsForUpdate[] = [
17171722
'label' => $rowLabels[$column][$columnImageKey],
1718-
'imageData' => $existingImages[$rowSku][$uploadedFile]
1723+
'imageData' => $currentFileData,
1724+
];
1725+
}
1726+
1727+
if (array_key_exists($uploadedFile, $imageHiddenStates)
1728+
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
1729+
) {
1730+
$imagesForChangeVisibility[] = [
1731+
'disabled' => $imageHiddenStates[$uploadedFile],
1732+
'imageData' => $currentFileData,
17191733
];
17201734
}
17211735
} else {
@@ -1724,9 +1738,11 @@ protected function _saveProducts()
17241738
}
17251739
$mediaGallery[$storeId][$rowSku][$uploadedFile] = [
17261740
'attribute_id' => $this->getMediaGalleryAttributeId(),
1727-
'label' => isset($rowLabels[$column][$columnImageKey]) ? $rowLabels[$column][$columnImageKey] : '',
1741+
'label' => isset($rowLabels[$column][$columnImageKey])
1742+
? $rowLabels[$column][$columnImageKey] : '',
17281743
'position' => ++$position,
1729-
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
1744+
'disabled' => isset($imageHiddenStates[$columnImage])
1745+
? $imageHiddenStates[$columnImage] : '0',
17301746
'value' => $uploadedFile,
17311747
];
17321748
}
@@ -1847,6 +1863,8 @@ protected function _saveProducts()
18471863
$mediaGallery
18481864
)->_saveProductAttributes(
18491865
$attributes
1866+
)->updateMediaGalleryVisibility(
1867+
$imagesForChangeVisibility
18501868
)->updateMediaGalleryLabels(
18511869
$labelsForUpdate
18521870
);
@@ -1861,6 +1879,34 @@ protected function _saveProducts()
18611879
}
18621880

18631881
/**
1882+
* Prepare array with image states (visible or hidden from product page)
1883+
*
1884+
* @param array $rowData
1885+
* @return array
1886+
*/
1887+
private function getImagesHiddenStates(array $rowData): array
1888+
{
1889+
$statesArray = [];
1890+
$mappingArray = [
1891+
'_media_is_disabled' => '1',
1892+
];
1893+
1894+
foreach ($mappingArray as $key => $value) {
1895+
if (isset($rowData[$key]) && strlen(trim($rowData[$key]))) {
1896+
$items = explode($this->getMultipleValueSeparator(), $rowData[$key]);
1897+
1898+
foreach ($items as $item) {
1899+
$statesArray[$item] = $value;
1900+
}
1901+
}
1902+
}
1903+
1904+
return $statesArray;
1905+
}
1906+
1907+
/**
1908+
* Resolve valid category ids from provided row data.
1909+
*
18641910
* @param array $rowData
18651911
* @return array
18661912
*/
@@ -2205,7 +2251,7 @@ public function getEntityTypeCode()
22052251
* Returns array of new products data with SKU as key. All SKU keys are in lowercase for avoiding creation of
22062252
* new products with the same SKU in different letter cases.
22072253
*
2208-
* @var string $sku
2254+
* @param string $sku
22092255
* @return array
22102256
*/
22112257
public function getNewSku($sku = null)
@@ -2782,6 +2828,21 @@ private function updateMediaGalleryLabels(array $labels)
27822828
$this->mediaProcessor->updateMediaGalleryLabels($labels);
27832829
}
27842830

2831+
/**
2832+
* Update 'disabled' field for media gallery entity
2833+
*
2834+
* @param array $images
2835+
* @return $this
2836+
*/
2837+
private function updateMediaGalleryVisibility(array $images): Product
2838+
{
2839+
if (!empty($images)) {
2840+
$this->mediaProcessor->updateMediaGalleryVisibility($images);
2841+
}
2842+
2843+
return $this;
2844+
}
2845+
27852846
/**
27862847
* Parse values from multiple attributes fields
27872848
*

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(
103103
/**
104104
* Save product media gallery.
105105
*
106-
* @param $mediaGalleryData
106+
* @param array $mediaGalleryData
107107
* @return void
108108
*/
109109
public function saveMediaGallery(array $mediaGalleryData)
@@ -152,14 +152,37 @@ public function saveMediaGallery(array $mediaGalleryData)
152152
* @return void
153153
*/
154154
public function updateMediaGalleryLabels(array $labels)
155+
{
156+
$this->updateMediaGalleryField($labels, 'label');
157+
}
158+
159+
/**
160+
* Update 'disabled' field for media gallery entity
161+
*
162+
* @param array $images
163+
* @return void
164+
*/
165+
public function updateMediaGalleryVisibility(array $images)
166+
{
167+
$this->updateMediaGalleryField($images, 'disabled');
168+
}
169+
170+
/**
171+
* Update value for requested field in media gallery entities
172+
*
173+
* @param array $data
174+
* @param string $field
175+
* @return void
176+
*/
177+
private function updateMediaGalleryField(array $data, string $field)
155178
{
156179
$insertData = [];
157-
foreach ($labels as $label) {
158-
$imageData = $label['imageData'];
180+
foreach ($data as $datum) {
181+
$imageData = $datum['imageData'];
159182

160-
if ($imageData['label'] === null) {
183+
if ($imageData[$field] === null) {
161184
$insertData[] = [
162-
'label' => $label['label'],
185+
$field => $datum[$field],
163186
$this->getProductEntityLinkField() => $imageData[$this->getProductEntityLinkField()],
164187
'value_id' => $imageData['value_id'],
165188
'store_id' => Store::DEFAULT_STORE_ID,
@@ -168,7 +191,7 @@ public function updateMediaGalleryLabels(array $labels)
168191
$this->connection->update(
169192
$this->mediaGalleryValueTableName,
170193
[
171-
'label' => $label['label'],
194+
$field => $datum[$field],
172195
],
173196
[
174197
$this->getProductEntityLinkField() . ' = ?' => $imageData[$this->getProductEntityLinkField()],
@@ -224,6 +247,7 @@ public function getExistingImages(array $bunch)
224247
),
225248
[
226249
'label' => 'mgv.label',
250+
'disabled' => 'mgv.disabled',
227251
]
228252
)->joinInner(
229253
['pe' => $this->productEntityTableName],
@@ -263,7 +287,7 @@ private function initMediaGalleryResources()
263287
/**
264288
* Save media gallery data per store.
265289
*
266-
* @param $storeId
290+
* @param int $storeId
267291
* @param array $mediaGalleryData
268292
* @param array $newMediaValues
269293
* @param array $valueToProductId

0 commit comments

Comments
 (0)