Skip to content

Commit 58d2268

Browse files
author
Sergey Shvets
committed
MAGETWO-97625: [Magento Cloud] Can't delete video images on Duplicate products
1 parent 45e3ddb commit 58d2268

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ protected function duplicate($product)
318318

319319
$this->resourceModel->duplicate(
320320
$this->getAttribute()->getAttributeId(),
321-
isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [],
321+
$mediaGalleryData['duplicate'] ?? [],
322322
$product->getOriginalLinkId(),
323323
$product->getData($this->metadata->getLinkField())
324324
);

app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Model\ResourceModel\Product;
78

89
use Magento\Store\Model\Store;
@@ -149,7 +150,7 @@ public function loadProductGalleryByAttributeId($product, $attributeId)
149150
*/
150151
protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
151152
{
152-
$select = $this->createBatchBaseSelect($storeId, $attributeId);
153+
$select = $this->createBatchBaseSelect($storeId, $attributeId);
153154

154155
$select = $select->where(
155156
'entity.' . $this->metadata->getLinkField() . ' = ?',
@@ -378,9 +379,9 @@ public function deleteGalleryValueInStore($valueId, $entityId, $storeId)
378379
$conditions = implode(
379380
' AND ',
380381
[
381-
$this->getConnection()->quoteInto('value_id = ?', (int) $valueId),
382-
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int) $entityId),
383-
$this->getConnection()->quoteInto('store_id = ?', (int) $storeId)
382+
$this->getConnection()->quoteInto('value_id = ?', (int)$valueId),
383+
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int)$entityId),
384+
$this->getConnection()->quoteInto('store_id = ?', (int)$storeId)
384385
]
385386
);
386387

@@ -408,7 +409,7 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
408409

409410
$select = $this->getConnection()->select()->from(
410411
[$this->getMainTableAlias() => $this->getMainTable()],
411-
['value_id', 'value']
412+
['value_id', 'value', 'media_type', 'disabled']
412413
)->joinInner(
413414
['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
414415
$this->getMainTableAlias() . '.value_id = entity.value_id',
@@ -425,16 +426,16 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
425426

426427
// Duplicate main entries of gallery
427428
foreach ($this->getConnection()->fetchAll($select) as $row) {
428-
$data = [
429-
'attribute_id' => $attributeId,
430-
'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'],
431-
];
429+
$data = $row;
430+
$data['attribute_id'] = $attributeId;
431+
$data['value'] = $newFiles[$row['value_id']] ?? $row['value'];
432+
unset($data['value_id']);
432433

433434
$valueIdMap[$row['value_id']] = $this->insertGallery($data);
434435
$this->bindValueToEntity($valueIdMap[$row['value_id']], $newProductId);
435436
}
436437

437-
if (count($valueIdMap) == 0) {
438+
if (count($valueIdMap) === 0) {
438439
return [];
439440
}
440441

app/code/Magento/ProductVideo/Model/Plugin/Catalog/Product/Gallery/CreateHandler.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function afterExecute(
5858
);
5959

6060
if (!empty($mediaCollection)) {
61+
if ($product->getIsDuplicate() === true) {
62+
$mediaCollection = $this->makeAllNewVideos($product->getId(), $mediaCollection);
63+
}
6164
$newVideoCollection = $this->collectNewVideos($mediaCollection);
6265
$this->saveVideoData($newVideoCollection, 0);
6366

@@ -282,4 +285,22 @@ private function isNewVideo(array $item): bool
282285
|| empty($item['video_url_default'])
283286
|| empty($item['video_title_default']);
284287
}
288+
289+
/**
290+
*
291+
* @param int $entityId
292+
* @param array $mediaCollection
293+
* @return array
294+
*/
295+
private function makeAllNewVideos($entityId, array $mediaCollection): array
296+
{
297+
foreach ($mediaCollection as $key => $video) {
298+
if ($this->isVideoItem($video)) {
299+
unset($video['video_url_default'], $video['video_title_default']);
300+
$video['entity_id'] = $entityId;
301+
$mediaCollection[$key] = $video;
302+
}
303+
}
304+
return $mediaCollection;
305+
}
285306
}

0 commit comments

Comments
 (0)