|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Model\Plugin\SpecialPricePluginForREST; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\Product\Price\SpecialPriceStorage; |
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Special price storage Plugin to handle website scope issue at the frontend (only for REST API calls) |
| 13 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 14 | + */ |
| 15 | +class SpecialPriceStoragePlugin |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private StoreManagerInterface $storeManager |
| 19 | + ) {} |
| 20 | + |
| 21 | + public function aroundUpdate(SpecialPriceStorage $subject, callable $proceed, array $prices) |
| 22 | + { |
| 23 | + $prices = $this->applyWebsitePrices($prices); |
| 24 | + return $proceed($prices); |
| 25 | + } |
| 26 | + |
| 27 | + private function applyWebsitePrices(array $formattedPrices): array |
| 28 | + { |
| 29 | + $newPrices = []; |
| 30 | + |
| 31 | + foreach ($formattedPrices as $price) { |
| 32 | + // Add the original price first |
| 33 | + $newPrices[] = $price; |
| 34 | + |
| 35 | + if ($price->getStoreId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + $store = $this->storeManager->getStore($price->getStoreId()); |
| 40 | + $website = $store->getWebsite(); |
| 41 | + $storeIds = $website->getStoreIds(); |
| 42 | + |
| 43 | + // Unset origin store view to avoid duplication |
| 44 | + unset($storeIds[$price->getStoreId()]); |
| 45 | + |
| 46 | + foreach ($storeIds as $storeId) { |
| 47 | + /** @var \Magento\Catalog\Model\Product\Price\SpecialPrice $cloned */ |
| 48 | + $cloned = clone $price; |
| 49 | + $cloned->setStoreId((int) $storeId); |
| 50 | + $newPrices[] = $cloned; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return $newPrices; |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments