66
77namespace Magento \Catalog \Model \Product \Price ;
88
9+ use Magento \Catalog \Api \BasePriceStorageInterface ;
10+ use Magento \Catalog \Api \Data \BasePriceInterface ;
11+ use Magento \Catalog \Api \Data \BasePriceInterfaceFactory ;
12+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
13+ use Magento \Catalog \Api \ProductRepositoryInterface ;
14+ use Magento \Catalog \Model \Product \Price \Validation \InvalidSkuProcessor ;
15+ use Magento \Catalog \Model \Product \Price \Validation \Result ;
16+ use Magento \Catalog \Model \ProductIdLocatorInterface ;
17+ use Magento \Framework \App \ObjectManager ;
18+ use Magento \Framework \Exception \NoSuchEntityException ;
19+ use Magento \Store \Api \StoreRepositoryInterface ;
20+ use Magento \Store \Model \Store ;
21+
922/**
1023 * Base prices storage.
24+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1125 */
12- class BasePriceStorage implements \ Magento \ Catalog \ Api \ BasePriceStorageInterface
26+ class BasePriceStorage implements BasePriceStorageInterface
1327{
1428 /**
15- * Attribute code.
29+ * Price attribute code.
1630 *
1731 * @var string
1832 */
@@ -24,27 +38,27 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
2438 private $ pricePersistence ;
2539
2640 /**
27- * @var \Magento\Catalog\Api\Data\ BasePriceInterfaceFactory
41+ * @var BasePriceInterfaceFactory
2842 */
2943 private $ basePriceInterfaceFactory ;
3044
3145 /**
32- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
46+ * @var ProductIdLocatorInterface
3347 */
3448 private $ productIdLocator ;
3549
3650 /**
37- * @var \Magento\Store\Api\ StoreRepositoryInterface
51+ * @var StoreRepositoryInterface
3852 */
3953 private $ storeRepository ;
4054
4155 /**
42- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
56+ * @var ProductRepositoryInterface
4357 */
4458 private $ productRepository ;
4559
4660 /**
47- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
61+ * @var Result
4862 */
4963 private $ validationResult ;
5064
@@ -54,43 +68,50 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
5468 private $ pricePersistenceFactory ;
5569
5670 /**
57- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
71+ * @var InvalidSkuProcessor
5872 */
5973 private $ invalidSkuProcessor ;
6074
6175 /**
62- * Price type allowed.
76+ * @var ProductAttributeRepositoryInterface
77+ */
78+ private $ productAttributeRepository ;
79+
80+ /**
81+ * Is price type allowed
6382 *
6483 * @var int
6584 */
6685 private $ priceTypeAllowed = 1 ;
6786
6887 /**
69- * Allowed product types.
88+ * Array of allowed product types.
7089 *
7190 * @var array
7291 */
7392 private $ allowedProductTypes = [];
7493
7594 /**
7695 * @param PricePersistenceFactory $pricePersistenceFactory
77- * @param \Magento\Catalog\Api\Data\ BasePriceInterfaceFactory $basePriceInterfaceFactory
78- * @param \Magento\Catalog\Model\ ProductIdLocatorInterface $productIdLocator
79- * @param \Magento\Store\Api\ StoreRepositoryInterface $storeRepository
80- * @param \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository
81- * @param \Magento\Catalog\Model\Product\Price\Validation\ Result $validationResult
82- * @param \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor $invalidSkuProcessor
96+ * @param BasePriceInterfaceFactory $basePriceInterfaceFactory
97+ * @param ProductIdLocatorInterface $productIdLocator
98+ * @param StoreRepositoryInterface $storeRepository
99+ * @param ProductRepositoryInterface $productRepository
100+ * @param Result $validationResult
101+ * @param InvalidSkuProcessor $invalidSkuProcessor
83102 * @param array $allowedProductTypes [optional]
103+ * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
84104 */
85105 public function __construct (
86106 PricePersistenceFactory $ pricePersistenceFactory ,
87- \Magento \Catalog \Api \Data \BasePriceInterfaceFactory $ basePriceInterfaceFactory ,
88- \Magento \Catalog \Model \ProductIdLocatorInterface $ productIdLocator ,
89- \Magento \Store \Api \StoreRepositoryInterface $ storeRepository ,
90- \Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
91- \Magento \Catalog \Model \Product \Price \Validation \Result $ validationResult ,
92- \Magento \Catalog \Model \Product \Price \Validation \InvalidSkuProcessor $ invalidSkuProcessor ,
93- array $ allowedProductTypes = []
107+ BasePriceInterfaceFactory $ basePriceInterfaceFactory ,
108+ ProductIdLocatorInterface $ productIdLocator ,
109+ StoreRepositoryInterface $ storeRepository ,
110+ ProductRepositoryInterface $ productRepository ,
111+ Result $ validationResult ,
112+ InvalidSkuProcessor $ invalidSkuProcessor ,
113+ array $ allowedProductTypes = [],
114+ ProductAttributeRepositoryInterface $ productAttributeRepository = null
94115 ) {
95116 $ this ->pricePersistenceFactory = $ pricePersistenceFactory ;
96117 $ this ->basePriceInterfaceFactory = $ basePriceInterfaceFactory ;
@@ -100,10 +121,12 @@ public function __construct(
100121 $ this ->validationResult = $ validationResult ;
101122 $ this ->allowedProductTypes = $ allowedProductTypes ;
102123 $ this ->invalidSkuProcessor = $ invalidSkuProcessor ;
124+ $ this ->productAttributeRepository = $ productAttributeRepository ?: ObjectManager::getInstance ()
125+ ->get (ProductAttributeRepositoryInterface::class);
103126 }
104127
105128 /**
106- * { @inheritdoc}
129+ * @inheritdoc
107130 */
108131 public function get (array $ skus )
109132 {
@@ -128,7 +151,7 @@ public function get(array $skus)
128151 }
129152
130153 /**
131- * { @inheritdoc}
154+ * @inheritdoc
132155 */
133156 public function update (array $ prices )
134157 {
@@ -146,6 +169,12 @@ public function update(array $prices)
146169 }
147170 }
148171
172+ $ priceAttribute = $ this ->productAttributeRepository ->get ($ this ->attributeCode );
173+
174+ if ($ priceAttribute !== null && $ priceAttribute ->isScopeWebsite ()) {
175+ $ formattedPrices = $ this ->applyWebsitePrices ($ formattedPrices );
176+ }
177+
149178 $ this ->getPricePersistence ()->update ($ formattedPrices );
150179
151180 return $ this ->validationResult ->getFailedItems ();
@@ -168,7 +197,7 @@ private function getPricePersistence()
168197 /**
169198 * Retrieve valid prices that do not contain any errors.
170199 *
171- * @param \Magento\Catalog\Api\Data\ BasePriceInterface[] $prices
200+ * @param BasePriceInterface[] $prices
172201 * @return array
173202 */
174203 private function retrieveValidPrices (array $ prices )
@@ -207,7 +236,7 @@ private function retrieveValidPrices(array $prices)
207236 }
208237 try {
209238 $ this ->storeRepository ->getById ($ price ->getStoreId ());
210- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
239+ } catch (NoSuchEntityException $ e ) {
211240 $ this ->validationResult ->addFailedItem (
212241 $ id ,
213242 __ (
@@ -225,4 +254,32 @@ private function retrieveValidPrices(array $prices)
225254
226255 return $ prices ;
227256 }
257+
258+ /**
259+ * If Catalog Price Mode is Website, price needs to be applied to all Store Views in this website.
260+ *
261+ * @param array $formattedPrices
262+ * @return array
263+ * @throws NoSuchEntityException
264+ */
265+ private function applyWebsitePrices ($ formattedPrices ): array
266+ {
267+ foreach ($ formattedPrices as $ price ) {
268+ if ($ price ['store_id ' ] == Store::DEFAULT_STORE_ID ) {
269+ continue ;
270+ }
271+
272+ $ storeIds = $ this ->storeRepository ->getById ($ price ['store_id ' ])->getWebsite ()->getStoreIds ();
273+
274+ // Unset origin store view to get rid of duplicate
275+ unset($ storeIds [$ price ['store_id ' ]]);
276+
277+ foreach ($ storeIds as $ storeId ) {
278+ $ price ['store_id ' ] = (int )$ storeId ;
279+ $ formattedPrices [] = $ price ;
280+ }
281+ }
282+
283+ return $ formattedPrices ;
284+ }
228285}
0 commit comments