-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6943 from magento-l3/PR_20210609_1
L3 Bugfix delivery. 2.4-develop
- Loading branch information
Showing
23 changed files
with
846 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
app/code/Magento/Catalog/Model/Product/Gallery/CopyHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Product\Gallery; | ||
|
||
use Magento\Catalog\Api\Data\ProductAttributeInterface; | ||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\ResourceModel\Product\Gallery; | ||
use Magento\Eav\Model\ResourceModel\AttributeValue; | ||
use Magento\Framework\EntityManager\EntityMetadata; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\Framework\EntityManager\Operation\ExtensionInterface; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
/** | ||
* Copy gallery data from one product to another | ||
*/ | ||
class CopyHandler implements ExtensionInterface | ||
{ | ||
/** | ||
* @var EntityMetadata | ||
*/ | ||
private $metadata; | ||
|
||
/** | ||
* @var Gallery | ||
*/ | ||
private $galleryResourceModel; | ||
|
||
/** | ||
* @var ProductAttributeRepositoryInterface | ||
*/ | ||
private $attributeRepository; | ||
|
||
/** | ||
* @var AttributeValue | ||
*/ | ||
private $attributeValue; | ||
|
||
/** | ||
* @var Json | ||
*/ | ||
private $json; | ||
|
||
/** | ||
* @var ProductAttributeInterface | ||
*/ | ||
private $attribute; | ||
|
||
/** | ||
* @param MetadataPool $metadataPool | ||
* @param Gallery $galleryResourceModel | ||
* @param ProductAttributeRepositoryInterface $attributeRepository | ||
* @param AttributeValue $attributeValue | ||
* @param Json $json | ||
*/ | ||
public function __construct( | ||
MetadataPool $metadataPool, | ||
Gallery $galleryResourceModel, | ||
ProductAttributeRepositoryInterface $attributeRepository, | ||
AttributeValue $attributeValue, | ||
Json $json | ||
) { | ||
$this->metadata = $metadataPool->getMetadata(ProductInterface::class); | ||
$this->galleryResourceModel = $galleryResourceModel; | ||
$this->attributeRepository = $attributeRepository; | ||
$this->attributeValue = $attributeValue; | ||
$this->json = $json; | ||
} | ||
|
||
/** | ||
* Copy gallery data from one product to another | ||
* | ||
* @param Product $product | ||
* @param array $arguments | ||
* @return void | ||
*/ | ||
public function execute($product, $arguments = []): void | ||
{ | ||
$fromId = (int) $arguments['original_link_id']; | ||
$toId = $product->getData($this->metadata->getLinkField()); | ||
$attributeId = $this->getAttribute()->getAttributeId(); | ||
$valueIdMap = $this->galleryResourceModel->duplicate($attributeId, [], $fromId, $toId); | ||
$gallery = $this->getMediaGalleryCollection($product); | ||
|
||
if (!empty($gallery['images'])) { | ||
$images = []; | ||
foreach ($gallery['images'] as $key => $image) { | ||
$valueId = $image['value_id'] ?? null; | ||
$newKey = $key; | ||
if ($valueId !== null) { | ||
$newValueId = $valueId; | ||
if (isset($valueIdMap[$valueId])) { | ||
$newValueId = $valueIdMap[$valueId]; | ||
} | ||
if (((int) $valueId) === $key) { | ||
$newKey = $newValueId; | ||
} | ||
$image['value_id'] = $newValueId; | ||
} | ||
$images[$newKey] = $image; | ||
} | ||
$gallery['images'] = $images; | ||
$attrCode = $this->getAttribute()->getAttributeCode(); | ||
$product->setData($attrCode, $gallery); | ||
} | ||
|
||
//Copy media attribute values from one product to another | ||
if (isset($arguments['media_attribute_codes'])) { | ||
$values = $this->attributeValue->getValues( | ||
ProductInterface::class, | ||
$fromId, | ||
$arguments['media_attribute_codes'] | ||
); | ||
if ($values) { | ||
foreach (array_keys($values) as $key) { | ||
$values[$key][$this->metadata->getLinkField()] = $product->getData($this->metadata->getLinkField()); | ||
unset($values[$key]['value_id']); | ||
} | ||
$this->attributeValue->insertValues( | ||
ProductInterface::class, | ||
$values | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get product media gallery collection | ||
* | ||
* @param Product $product | ||
* @return array | ||
*/ | ||
private function getMediaGalleryCollection(Product $product): array | ||
{ | ||
$attrCode = $this->getAttribute()->getAttributeCode(); | ||
$value = $product->getData($attrCode); | ||
|
||
if (is_array($value) && isset($value['images'])) { | ||
if (!is_array($value['images']) && strlen($value['images']) > 0) { | ||
$value['images'] = $this->json->unserialize($value['images']); | ||
} | ||
|
||
if (!is_array($value['images'])) { | ||
$value['images'] = []; | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Returns media gallery attribute instance | ||
* | ||
* @return ProductAttributeInterface | ||
*/ | ||
private function getAttribute(): ProductAttributeInterface | ||
{ | ||
if (!$this->attribute) { | ||
$this->attribute = $this->attributeRepository->get( | ||
ProductInterface::MEDIA_GALLERY | ||
); | ||
} | ||
|
||
return $this->attribute; | ||
} | ||
} |
Oops, something went wrong.