6
6
7
7
namespace Magento \Catalog \Model \Product \Price ;
8
8
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
+
9
22
/**
10
23
* Base prices storage.
11
24
*/
12
- class BasePriceStorage implements \ Magento \ Catalog \ Api \ BasePriceStorageInterface
25
+ class BasePriceStorage implements BasePriceStorageInterface
13
26
{
14
27
/**
15
28
* Attribute code.
@@ -24,27 +37,27 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
24
37
private $ pricePersistence ;
25
38
26
39
/**
27
- * @var \Magento\Catalog\Api\Data\ BasePriceInterfaceFactory
40
+ * @var BasePriceInterfaceFactory
28
41
*/
29
42
private $ basePriceInterfaceFactory ;
30
43
31
44
/**
32
- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
45
+ * @var ProductIdLocatorInterface
33
46
*/
34
47
private $ productIdLocator ;
35
48
36
49
/**
37
- * @var \Magento\Store\Api\ StoreRepositoryInterface
50
+ * @var StoreRepositoryInterface
38
51
*/
39
52
private $ storeRepository ;
40
53
41
54
/**
42
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
55
+ * @var ProductRepositoryInterface
43
56
*/
44
57
private $ productRepository ;
45
58
46
59
/**
47
- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
60
+ * @var Result
48
61
*/
49
62
private $ validationResult ;
50
63
@@ -54,10 +67,15 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
54
67
private $ pricePersistenceFactory ;
55
68
56
69
/**
57
- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
70
+ * @var InvalidSkuProcessor
58
71
*/
59
72
private $ invalidSkuProcessor ;
60
73
74
+ /**
75
+ * @var ProductAttributeRepositoryInterface
76
+ */
77
+ private $ productAttributeRepository ;
78
+
61
79
/**
62
80
* Price type allowed.
63
81
*
@@ -74,23 +92,25 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
74
92
75
93
/**
76
94
* @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
95
+ * @param BasePriceInterfaceFactory $basePriceInterfaceFactory
96
+ * @param ProductIdLocatorInterface $productIdLocator
97
+ * @param StoreRepositoryInterface $storeRepository
98
+ * @param ProductRepositoryInterface $productRepository
99
+ * @param Result $validationResult
100
+ * @param InvalidSkuProcessor $invalidSkuProcessor
101
+ * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
83
102
* @param array $allowedProductTypes [optional]
84
103
*/
85
104
public function __construct (
86
105
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 = []
106
+ BasePriceInterfaceFactory $ basePriceInterfaceFactory ,
107
+ ProductIdLocatorInterface $ productIdLocator ,
108
+ StoreRepositoryInterface $ storeRepository ,
109
+ ProductRepositoryInterface $ productRepository ,
110
+ Result $ validationResult ,
111
+ InvalidSkuProcessor $ invalidSkuProcessor ,
112
+ array $ allowedProductTypes = [],
113
+ ProductAttributeRepositoryInterface $ productAttributeRepository = null
94
114
) {
95
115
$ this ->pricePersistenceFactory = $ pricePersistenceFactory ;
96
116
$ this ->basePriceInterfaceFactory = $ basePriceInterfaceFactory ;
@@ -100,10 +120,12 @@ public function __construct(
100
120
$ this ->validationResult = $ validationResult ;
101
121
$ this ->allowedProductTypes = $ allowedProductTypes ;
102
122
$ this ->invalidSkuProcessor = $ invalidSkuProcessor ;
123
+ $ this ->productAttributeRepository = $ productAttributeRepository ?: ObjectManager::getInstance ()
124
+ ->get (ProductAttributeRepositoryInterface::class);
103
125
}
104
126
105
127
/**
106
- * { @inheritdoc}
128
+ * @inheritdoc
107
129
*/
108
130
public function get (array $ skus )
109
131
{
@@ -128,7 +150,7 @@ public function get(array $skus)
128
150
}
129
151
130
152
/**
131
- * { @inheritdoc}
153
+ * @inheritdoc
132
154
*/
133
155
public function update (array $ prices )
134
156
{
@@ -146,6 +168,12 @@ public function update(array $prices)
146
168
}
147
169
}
148
170
171
+ $ priceAttribute = $ this ->productAttributeRepository ->get ($ this ->attributeCode );
172
+
173
+ if ($ priceAttribute !== null && $ priceAttribute ->isScopeWebsite ()) {
174
+ $ formattedPrices = $ this ->applyWebsitePrices ($ formattedPrices );
175
+ }
176
+
149
177
$ this ->getPricePersistence ()->update ($ formattedPrices );
150
178
151
179
return $ this ->validationResult ->getFailedItems ();
@@ -168,7 +196,7 @@ private function getPricePersistence()
168
196
/**
169
197
* Retrieve valid prices that do not contain any errors.
170
198
*
171
- * @param \Magento\Catalog\Api\Data\ BasePriceInterface[] $prices
199
+ * @param BasePriceInterface[] $prices
172
200
* @return array
173
201
*/
174
202
private function retrieveValidPrices (array $ prices )
@@ -207,7 +235,7 @@ private function retrieveValidPrices(array $prices)
207
235
}
208
236
try {
209
237
$ this ->storeRepository ->getById ($ price ->getStoreId ());
210
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
238
+ } catch (NoSuchEntityException $ e ) {
211
239
$ this ->validationResult ->addFailedItem (
212
240
$ id ,
213
241
__ (
@@ -225,4 +253,32 @@ private function retrieveValidPrices(array $prices)
225
253
226
254
return $ prices ;
227
255
}
256
+
257
+ /**
258
+ * If Catalog Price Mode is Website, price needs to be applied to all Store Views in this website.
259
+ *
260
+ * @param array $formattedPrices
261
+ * @return array
262
+ * @throws NoSuchEntityException
263
+ */
264
+ private function applyWebsitePrices ($ formattedPrices ): array
265
+ {
266
+ foreach ($ formattedPrices as $ price ) {
267
+ if ($ price ['store_id ' ] == Store::DEFAULT_STORE_ID ) {
268
+ continue ;
269
+ }
270
+
271
+ $ storeIds = $ this ->storeRepository ->getById ($ price ['store_id ' ])->getWebsite ()->getStoreIds ();
272
+
273
+ // Unset origin store view to get rid of duplicate
274
+ unset($ storeIds [$ price ['store_id ' ]]);
275
+
276
+ foreach ($ storeIds as $ storeId ) {
277
+ $ price ['store_id ' ] = (int )$ storeId ;
278
+ $ formattedPrices [] = $ price ;
279
+ }
280
+ }
281
+
282
+ return $ formattedPrices ;
283
+ }
228
284
}
0 commit comments