|
8 | 8 | namespace Magento\Catalog\Model\Product\Gallery;
|
9 | 9 |
|
10 | 10 | use Magento\Catalog\Api\Data\ProductInterface;
|
| 11 | +use Magento\Catalog\Model\Product; |
11 | 12 | use Magento\Framework\App\Filesystem\DirectoryList;
|
12 | 13 | use Magento\Framework\App\ObjectManager;
|
13 | 14 | use Magento\Framework\EntityManager\Operation\ExtensionInterface;
|
14 | 15 | use Magento\MediaStorage\Model\File\Uploader as FileUploader;
|
| 16 | +use Magento\Store\Model\Store; |
15 | 17 | use Magento\Store\Model\StoreManagerInterface;
|
16 | 18 |
|
17 | 19 | /**
|
@@ -89,6 +91,15 @@ class CreateHandler implements ExtensionInterface
|
89 | 91 | */
|
90 | 92 | private $storeManager;
|
91 | 93 |
|
| 94 | + /** |
| 95 | + * @var string[] |
| 96 | + */ |
| 97 | + private $mediaAttributesWithLabels = [ |
| 98 | + 'image', |
| 99 | + 'small_image', |
| 100 | + 'thumbnail' |
| 101 | + ]; |
| 102 | + |
92 | 103 | /**
|
93 | 104 | * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
|
94 | 105 | * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
|
@@ -190,27 +201,8 @@ public function execute($product, $arguments = [])
|
190 | 201 | $value['duplicate'] = $duplicate;
|
191 | 202 | }
|
192 | 203 |
|
193 |
| - /* @var $mediaAttribute \Magento\Catalog\Api\Data\ProductAttributeInterface */ |
194 |
| - foreach ($this->getMediaAttributeCodes() as $mediaAttrCode) { |
195 |
| - $attrData = $product->getData($mediaAttrCode); |
196 |
| - if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) { |
197 |
| - continue; |
198 |
| - } |
199 |
| - $this->processMediaAttribute( |
200 |
| - $product, |
201 |
| - $mediaAttrCode, |
202 |
| - $clearImages, |
203 |
| - $newImages |
204 |
| - ); |
205 |
| - if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail'])) { |
206 |
| - $this->processMediaAttributeLabel( |
207 |
| - $product, |
208 |
| - $mediaAttrCode, |
209 |
| - $clearImages, |
210 |
| - $newImages, |
211 |
| - $existImages |
212 |
| - ); |
213 |
| - } |
| 204 | + if (!empty($value['images'])) { |
| 205 | + $this->processMediaAttributes($product, $existImages, $newImages, $clearImages); |
214 | 206 | }
|
215 | 207 |
|
216 | 208 | $product->setData($attrCode, $value);
|
@@ -492,50 +484,59 @@ private function getMediaAttributeCodes()
|
492 | 484 | /**
|
493 | 485 | * Process media attribute
|
494 | 486 | *
|
495 |
| - * @param \Magento\Catalog\Model\Product $product |
| 487 | + * @param Product $product |
496 | 488 | * @param string $mediaAttrCode
|
497 | 489 | * @param array $clearImages
|
498 | 490 | * @param array $newImages
|
499 | 491 | */
|
500 | 492 | private function processMediaAttribute(
|
501 |
| - \Magento\Catalog\Model\Product $product, |
502 |
| - $mediaAttrCode, |
| 493 | + Product $product, |
| 494 | + string $mediaAttrCode, |
503 | 495 | array $clearImages,
|
504 | 496 | array $newImages
|
505 |
| - ) { |
506 |
| - $attrData = $product->getData($mediaAttrCode); |
507 |
| - if (in_array($attrData, $clearImages)) { |
508 |
| - $product->setData($mediaAttrCode, 'no_selection'); |
509 |
| - } |
510 |
| - |
511 |
| - if (in_array($attrData, array_keys($newImages))) { |
512 |
| - $product->setData($mediaAttrCode, $newImages[$attrData]['new_file']); |
513 |
| - } |
514 |
| - if (!empty($product->getData($mediaAttrCode))) { |
| 497 | + ): void { |
| 498 | + $storeId = $product->isObjectNew() ? Store::DEFAULT_STORE_ID : (int) $product->getStoreId(); |
| 499 | + /*** |
| 500 | + * Attributes values are saved as default value in single store mode |
| 501 | + * @see \Magento\Catalog\Model\ResourceModel\AbstractResource::_saveAttributeValue |
| 502 | + */ |
| 503 | + if ($storeId === Store::DEFAULT_STORE_ID |
| 504 | + || $this->storeManager->hasSingleStore() |
| 505 | + || $this->getMediaAttributeStoreValue($product, $mediaAttrCode, $storeId) !== null |
| 506 | + ) { |
| 507 | + $value = $product->getData($mediaAttrCode); |
| 508 | + $newValue = $value; |
| 509 | + if (in_array($value, $clearImages)) { |
| 510 | + $newValue = 'no_selection'; |
| 511 | + } |
| 512 | + if (in_array($value, array_keys($newImages))) { |
| 513 | + $newValue = $newImages[$value]['new_file']; |
| 514 | + } |
| 515 | + $product->setData($mediaAttrCode, $newValue); |
515 | 516 | $product->addAttributeUpdate(
|
516 | 517 | $mediaAttrCode,
|
517 |
| - $product->getData($mediaAttrCode), |
518 |
| - $product->getStoreId() |
| 518 | + $newValue, |
| 519 | + $storeId |
519 | 520 | );
|
520 | 521 | }
|
521 | 522 | }
|
522 | 523 |
|
523 | 524 | /**
|
524 | 525 | * Process media attribute label
|
525 | 526 | *
|
526 |
| - * @param \Magento\Catalog\Model\Product $product |
| 527 | + * @param Product $product |
527 | 528 | * @param string $mediaAttrCode
|
528 | 529 | * @param array $clearImages
|
529 | 530 | * @param array $newImages
|
530 | 531 | * @param array $existImages
|
531 | 532 | */
|
532 | 533 | private function processMediaAttributeLabel(
|
533 |
| - \Magento\Catalog\Model\Product $product, |
534 |
| - $mediaAttrCode, |
| 534 | + Product $product, |
| 535 | + string $mediaAttrCode, |
535 | 536 | array $clearImages,
|
536 | 537 | array $newImages,
|
537 | 538 | array $existImages
|
538 |
| - ) { |
| 539 | + ): void { |
539 | 540 | $resetLabel = false;
|
540 | 541 | $attrData = $product->getData($mediaAttrCode);
|
541 | 542 | if (in_array($attrData, $clearImages)) {
|
@@ -607,4 +608,57 @@ private function canRemoveImage(ProductInterface $product, string $imageFile) :b
|
607 | 608 |
|
608 | 609 | return $canRemoveImage;
|
609 | 610 | }
|
| 611 | + |
| 612 | + /** |
| 613 | + * Get media attribute value for store view |
| 614 | + * |
| 615 | + * @param Product $product |
| 616 | + * @param string $attributeCode |
| 617 | + * @param int|null $storeId |
| 618 | + * @return string|null |
| 619 | + */ |
| 620 | + private function getMediaAttributeStoreValue(Product $product, string $attributeCode, int $storeId = null): ?string |
| 621 | + { |
| 622 | + $gallery = $this->getImagesForAllStores($product); |
| 623 | + $storeId = $storeId === null ? (int) $product->getStoreId() : $storeId; |
| 624 | + foreach ($gallery as $image) { |
| 625 | + if ($image['attribute_code'] === $attributeCode && ((int)$image['store_id']) === $storeId) { |
| 626 | + return $image['filepath']; |
| 627 | + } |
| 628 | + } |
| 629 | + return null; |
| 630 | + } |
| 631 | + |
| 632 | + /** |
| 633 | + * Update media attributes |
| 634 | + * |
| 635 | + * @param Product $product |
| 636 | + * @param array $existImages |
| 637 | + * @param array $newImages |
| 638 | + * @param array $clearImages |
| 639 | + */ |
| 640 | + private function processMediaAttributes( |
| 641 | + Product $product, |
| 642 | + array $existImages, |
| 643 | + array $newImages, |
| 644 | + array $clearImages |
| 645 | + ): void { |
| 646 | + foreach ($this->getMediaAttributeCodes() as $mediaAttrCode) { |
| 647 | + $this->processMediaAttribute( |
| 648 | + $product, |
| 649 | + $mediaAttrCode, |
| 650 | + $clearImages, |
| 651 | + $newImages |
| 652 | + ); |
| 653 | + if (in_array($mediaAttrCode, $this->mediaAttributesWithLabels)) { |
| 654 | + $this->processMediaAttributeLabel( |
| 655 | + $product, |
| 656 | + $mediaAttrCode, |
| 657 | + $clearImages, |
| 658 | + $newImages, |
| 659 | + $existImages |
| 660 | + ); |
| 661 | + } |
| 662 | + } |
| 663 | + } |
610 | 664 | }
|
0 commit comments