Skip to content

Commit 8809faf

Browse files
committed
Merge branch '2.2-develop' into 2.2-develop-pr3
2 parents a7dfddb + 37f5661 commit 8809faf

File tree

15 files changed

+462
-42
lines changed

15 files changed

+462
-42
lines changed

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

Lines changed: 75 additions & 14 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)
@@ -2716,7 +2762,7 @@ protected function getProductUrlSuffix($storeId = null)
27162762
protected function getUrlKey($rowData)
27172763
{
27182764
if (!empty($rowData[self::URL_KEY])) {
2719-
return strtolower($rowData[self::URL_KEY]);
2765+
return $this->productUrl->formatUrlKey($rowData[self::URL_KEY]);
27202766
}
27212767

27222768
if (!empty($rowData[self::COL_NAME])) {
@@ -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

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

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ class Option extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
336336
*/
337337
private $optionTypeTitles;
338338

339+
/**
340+
* @var array
341+
*/
342+
private $lastOptionTitle;
343+
339344
/**
340345
* @param \Magento\ImportExport\Model\ResourceModel\Import\Data $importData
341346
* @param ResourceConnection $resource
@@ -1119,6 +1124,8 @@ protected function _getMultiRowFormat($rowData)
11191124
}
11201125

11211126
/**
1127+
* Process option row.
1128+
*
11221129
* @param string $name
11231130
* @param array $optionRow
11241131
* @return array
@@ -1185,6 +1192,7 @@ private function addFileOptions($result, $optionRow)
11851192

11861193
/**
11871194
* Import data rows.
1195+
*
11881196
* Additional store view data (option titles) will be sought in store view specified import file rows
11891197
*
11901198
* @return boolean
@@ -1212,7 +1220,6 @@ protected function _importData()
12121220
$parentCount = [];
12131221
$childCount = [];
12141222
$optionsToRemove = [];
1215-
12161223
foreach ($bunch as $rowNumber => $rowData) {
12171224
if (isset($optionId, $valueId) && empty($rowData[Product::COL_STORE_VIEW_CODE])) {
12181225
$nextOptionId = $optionId;
@@ -1257,9 +1264,17 @@ protected function _importData()
12571264
$childCount
12581265
);
12591266
$this->_collectOptionTitle($combinedData, $prevOptionId, $titles);
1267+
$this->checkOptionTitles(
1268+
$options,
1269+
$titles,
1270+
$combinedData,
1271+
$prevOptionId,
1272+
$optionId,
1273+
$products,
1274+
$prices
1275+
);
12601276
}
12611277
}
1262-
12631278
// Remove all existing options if import behaviour is APPEND
12641279
// in other case remove options for products with empty "custom_options" row only
12651280
if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
@@ -1268,21 +1283,80 @@ protected function _importData()
12681283
// Remove options for products with empty "custom_options" row
12691284
$this->_deleteEntities($optionsToRemove);
12701285
}
1271-
12721286
// Save prepared custom options data
12731287
if ($this->_isReadyForSaving($options, $titles, $typeValues)) {
12741288
$types = [
12751289
'values' => $typeValues,
12761290
'prices' => $typePrices,
12771291
'titles' => $typeTitles
12781292
];
1293+
$this->setLastOptionTitle($titles);
12791294
$this->savePreparedCustomOptions($products, $options, $titles, $prices, $types);
12801295
}
12811296
}
12821297

12831298
return true;
12841299
}
12851300

1301+
/**
1302+
* Check options titles.
1303+
*
1304+
* If products were split up between bunches,
1305+
* this function will add needed option for option titles.
1306+
*
1307+
* @param array $options
1308+
* @param array $titles
1309+
* @param array $combinedData
1310+
* @param int $prevOptionId
1311+
* @param int $optionId
1312+
* @param array $products
1313+
* @param array $prices
1314+
* @return void
1315+
*/
1316+
private function checkOptionTitles(
1317+
array &$options,
1318+
array &$titles,
1319+
array $combinedData,
1320+
int &$prevOptionId,
1321+
int &$optionId,
1322+
array $products,
1323+
array $prices
1324+
) {
1325+
$titlesCount = count($titles);
1326+
if ($titlesCount > 0 && count($options) !== $titlesCount) {
1327+
$combinedData[Product::COL_STORE_VIEW_CODE] = '';
1328+
$optionId--;
1329+
$option = $this->_collectOptionMainData(
1330+
$combinedData,
1331+
$prevOptionId,
1332+
$optionId,
1333+
$products,
1334+
$prices
1335+
);
1336+
if ($option) {
1337+
$options[] = $option;
1338+
}
1339+
}
1340+
}
1341+
1342+
/**
1343+
* Setting last Custom Option Title
1344+
* to use it later in _collectOptionTitle
1345+
* to set correct title for default store view.
1346+
*
1347+
* @param array $titles
1348+
* @return void
1349+
*/
1350+
private function setLastOptionTitle(array &$titles)
1351+
{
1352+
if (count($titles) > 0) {
1353+
end($titles);
1354+
$key = key($titles);
1355+
$this->lastOptionTitle[$key] = $titles[$key];
1356+
}
1357+
}
1358+
1359+
12861360
/**
12871361
* Load data of existed products
12881362
*
@@ -1413,8 +1487,12 @@ protected function _collectOptionTitle(array $rowData, $prevOptionId, array &$ti
14131487
$defaultStoreId = Store::DEFAULT_STORE_ID;
14141488
if (!empty($rowData[self::COLUMN_TITLE])) {
14151489
if (!isset($titles[$prevOptionId][$defaultStoreId])) {
1416-
// ensure default title is set
1417-
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
1490+
if (isset($this->lastOptionTitle[$prevOptionId])) {
1491+
$titles[$prevOptionId] = $this->lastOptionTitle[$prevOptionId];
1492+
unset($this->lastOptionTitle);
1493+
} else {
1494+
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
1495+
}
14181496
}
14191497
$titles[$prevOptionId][$this->_rowStoreId] = $rowData[self::COLUMN_TITLE];
14201498
}

0 commit comments

Comments
 (0)