9
9
use Magento \Framework \DataObject ;
10
10
use Magento \Framework \Model \AbstractModel ;
11
11
use Magento \Store \Model \Store ;
12
+ use Magento \Tests \NamingConvention \true \float ;
12
13
13
14
/**
14
15
* Catalog product custom option resource model
@@ -95,8 +96,6 @@ protected function _afterSave(AbstractModel $object)
95
96
*
96
97
* @param AbstractModel $object
97
98
* @return $this
98
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
99
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
100
99
*/
101
100
protected function _saveValuePrices (AbstractModel $ object )
102
101
{
@@ -116,27 +115,13 @@ protected function _saveValuePrices(AbstractModel $object)
116
115
);
117
116
118
117
if ($ object ->getStoreId () != '0 ' && $ scope == Store::PRICE_SCOPE_WEBSITE ) {
119
- $ baseCurrency = $ this ->_config ->getValue (
120
- \Magento \Directory \Model \Currency::XML_PATH_CURRENCY_BASE ,
121
- 'default '
122
- );
123
-
124
118
$ storeIds = $ this ->_storeManager ->getStore ($ object ->getStoreId ())->getWebsite ()->getStoreIds ();
125
- if (is_array ($ storeIds )) {
126
- foreach ($ storeIds as $ storeId ) {
127
- if ($ object ->getPriceType () == 'fixed ' ) {
128
- $ storeCurrency = $ this ->_storeManager ->getStore ($ storeId )->getBaseCurrencyCode ();
129
- $ rate = $ this ->_currencyFactory ->create ()->load ($ baseCurrency )->getRate ($ storeCurrency );
130
- if (!$ rate ) {
131
- $ rate = 1 ;
132
- }
133
- $ newPrice = $ object ->getPrice () * $ rate ;
134
- } else {
135
- $ newPrice = $ object ->getPrice ();
136
- }
137
-
138
- $ this ->savePriceByStore ($ object , (int )$ storeId , $ newPrice );
139
- }
119
+ if (empty ($ storeIds )) {
120
+ return $ this ;
121
+ }
122
+ foreach ($ storeIds as $ storeId ) {
123
+ $ newPrice = $ this ->calculateStorePrice ($ object , $ storeId );
124
+ $ this ->savePriceByStore ($ object , (int )$ storeId , $ newPrice );
140
125
}
141
126
} elseif ($ scope == Store::PRICE_SCOPE_WEBSITE && $ object ->getData ('scope ' , 'price ' )) {
142
127
$ this ->getConnection ()->delete (
@@ -169,12 +154,14 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
169
154
170
155
if (!$ optionId ) {
171
156
$ data = $ this ->_prepareDataForTable (
172
- new DataObject ([
173
- 'option_id ' => $ object ->getId (),
174
- 'store_id ' => $ storeId ,
175
- 'price ' => $ price ,
176
- 'price_type ' => $ object ->getPriceType (),
177
- ]),
157
+ new DataObject (
158
+ [
159
+ 'option_id ' => $ object ->getId (),
160
+ 'store_id ' => $ storeId ,
161
+ 'price ' => $ price ,
162
+ 'price_type ' => $ object ->getPriceType (),
163
+ ]
164
+ ),
178
165
$ priceTable
179
166
);
180
167
$ connection ->insert ($ priceTable , $ data );
@@ -185,10 +172,12 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
185
172
}
186
173
187
174
$ data = $ this ->_prepareDataForTable (
188
- new DataObject ([
189
- 'price ' => $ price ,
190
- 'price_type ' => $ object ->getPriceType ()
191
- ]),
175
+ new DataObject (
176
+ [
177
+ 'price ' => $ price ,
178
+ 'price_type ' => $ object ->getPriceType ()
179
+ ]
180
+ ),
192
181
$ priceTable
193
182
);
194
183
@@ -203,6 +192,29 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
203
192
}
204
193
}
205
194
195
+ /**
196
+ * Calculate price by store
197
+ *
198
+ * @param AbstractModel $object
199
+ * @param int $storeId
200
+ * @return float
201
+ */
202
+ private function calculateStorePrice (AbstractModel $ object , int $ storeId ): float
203
+ {
204
+ $ price = $ object ->getPrice ();
205
+ if ($ object ->getPriceType () == 'fixed ' ) {
206
+ $ baseCurrency = $ this ->_config ->getValue (
207
+ \Magento \Directory \Model \Currency::XML_PATH_CURRENCY_BASE ,
208
+ 'default '
209
+ );
210
+ $ storeCurrency = $ this ->_storeManager ->getStore ($ storeId )->getBaseCurrencyCode ();
211
+ $ rate = $ this ->_currencyFactory ->create ()->load ($ baseCurrency )->getRate ($ storeCurrency );
212
+ $ price = $ object ->getPrice () * ($ rate ?: 1 );
213
+ }
214
+
215
+ return (float )$ price ;
216
+ }
217
+
206
218
/**
207
219
* Save titles
208
220
*
0 commit comments