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.
24
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11
25
*/
12
- class BasePriceStorage implements \ Magento \ Catalog \ Api \ BasePriceStorageInterface
26
+ class BasePriceStorage implements BasePriceStorageInterface
13
27
{
14
28
/**
15
- * Attribute code.
29
+ * Price attribute code.
16
30
*
17
31
* @var string
18
32
*/
@@ -24,27 +38,27 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
24
38
private $ pricePersistence ;
25
39
26
40
/**
27
- * @var \Magento\Catalog\Api\Data\ BasePriceInterfaceFactory
41
+ * @var BasePriceInterfaceFactory
28
42
*/
29
43
private $ basePriceInterfaceFactory ;
30
44
31
45
/**
32
- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
46
+ * @var ProductIdLocatorInterface
33
47
*/
34
48
private $ productIdLocator ;
35
49
36
50
/**
37
- * @var \Magento\Store\Api\ StoreRepositoryInterface
51
+ * @var StoreRepositoryInterface
38
52
*/
39
53
private $ storeRepository ;
40
54
41
55
/**
42
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
56
+ * @var ProductRepositoryInterface
43
57
*/
44
58
private $ productRepository ;
45
59
46
60
/**
47
- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
61
+ * @var Result
48
62
*/
49
63
private $ validationResult ;
50
64
@@ -54,43 +68,50 @@ class BasePriceStorage implements \Magento\Catalog\Api\BasePriceStorageInterface
54
68
private $ pricePersistenceFactory ;
55
69
56
70
/**
57
- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
71
+ * @var InvalidSkuProcessor
58
72
*/
59
73
private $ invalidSkuProcessor ;
60
74
61
75
/**
62
- * Price type allowed.
76
+ * @var ProductAttributeRepositoryInterface
77
+ */
78
+ private $ productAttributeRepository ;
79
+
80
+ /**
81
+ * Is price type allowed
63
82
*
64
83
* @var int
65
84
*/
66
85
private $ priceTypeAllowed = 1 ;
67
86
68
87
/**
69
- * Allowed product types.
88
+ * Array of allowed product types.
70
89
*
71
90
* @var array
72
91
*/
73
92
private $ allowedProductTypes = [];
74
93
75
94
/**
76
95
* @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
83
102
* @param array $allowedProductTypes [optional]
103
+ * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
84
104
*/
85
105
public function __construct (
86
106
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
94
115
) {
95
116
$ this ->pricePersistenceFactory = $ pricePersistenceFactory ;
96
117
$ this ->basePriceInterfaceFactory = $ basePriceInterfaceFactory ;
@@ -100,10 +121,12 @@ public function __construct(
100
121
$ this ->validationResult = $ validationResult ;
101
122
$ this ->allowedProductTypes = $ allowedProductTypes ;
102
123
$ this ->invalidSkuProcessor = $ invalidSkuProcessor ;
124
+ $ this ->productAttributeRepository = $ productAttributeRepository ?: ObjectManager::getInstance ()
125
+ ->get (ProductAttributeRepositoryInterface::class);
103
126
}
104
127
105
128
/**
106
- * { @inheritdoc}
129
+ * @inheritdoc
107
130
*/
108
131
public function get (array $ skus )
109
132
{
@@ -128,7 +151,7 @@ public function get(array $skus)
128
151
}
129
152
130
153
/**
131
- * { @inheritdoc}
154
+ * @inheritdoc
132
155
*/
133
156
public function update (array $ prices )
134
157
{
@@ -146,6 +169,12 @@ public function update(array $prices)
146
169
}
147
170
}
148
171
172
+ $ priceAttribute = $ this ->productAttributeRepository ->get ($ this ->attributeCode );
173
+
174
+ if ($ priceAttribute !== null && $ priceAttribute ->isScopeWebsite ()) {
175
+ $ formattedPrices = $ this ->applyWebsitePrices ($ formattedPrices );
176
+ }
177
+
149
178
$ this ->getPricePersistence ()->update ($ formattedPrices );
150
179
151
180
return $ this ->validationResult ->getFailedItems ();
@@ -168,7 +197,7 @@ private function getPricePersistence()
168
197
/**
169
198
* Retrieve valid prices that do not contain any errors.
170
199
*
171
- * @param \Magento\Catalog\Api\Data\ BasePriceInterface[] $prices
200
+ * @param BasePriceInterface[] $prices
172
201
* @return array
173
202
*/
174
203
private function retrieveValidPrices (array $ prices )
@@ -207,7 +236,7 @@ private function retrieveValidPrices(array $prices)
207
236
}
208
237
try {
209
238
$ this ->storeRepository ->getById ($ price ->getStoreId ());
210
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
239
+ } catch (NoSuchEntityException $ e ) {
211
240
$ this ->validationResult ->addFailedItem (
212
241
$ id ,
213
242
__ (
@@ -225,4 +254,32 @@ private function retrieveValidPrices(array $prices)
225
254
226
255
return $ prices ;
227
256
}
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
+ }
228
285
}
0 commit comments