5
5
*/
6
6
namespace Magento \Catalog \Model \ResourceModel \Product \Option ;
7
7
8
+ use Magento \Catalog \Model \Product \Option \Value as OptionValue ;
9
+ use Magento \Directory \Model \Currency ;
10
+ use Magento \Directory \Model \CurrencyFactory ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Framework \Locale \FormatInterface ;
14
+ use Magento \Framework \Model \AbstractModel ;
15
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
16
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
17
+ use Magento \Store \Model \ScopeInterface ;
18
+ use Magento \Store \Model \Store ;
19
+ use Magento \Store \Model \StoreManagerInterface ;
20
+
8
21
/**
9
22
* Catalog product custom option resource model
10
23
*
11
24
* @author Magento Core Team <[email protected] >
12
25
*/
13
- class Value extends \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb
26
+ class Value extends AbstractDb
14
27
{
15
28
/**
16
29
* Store manager
17
30
*
18
- * @var \Magento\Store\Model\ StoreManagerInterface
31
+ * @var StoreManagerInterface
19
32
*/
20
33
protected $ _storeManager ;
21
34
22
35
/**
23
36
* Currency factory
24
37
*
25
- * @var \Magento\Directory\Model\ CurrencyFactory
38
+ * @var CurrencyFactory
26
39
*/
27
40
protected $ _currencyFactory ;
28
41
29
42
/**
30
43
* Core config model
31
44
*
32
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
45
+ * @var ScopeConfigInterface
33
46
*/
34
47
protected $ _config ;
35
48
36
49
/**
37
- * @var \Magento\Framework\Locale\ FormatInterface
50
+ * @var FormatInterface
38
51
*/
39
52
private $ localeFormat ;
40
53
41
54
/**
42
55
* Class constructor
43
56
*
44
- * @param \Magento\Framework\Model\ResourceModel\Db\ Context $context
45
- * @param \Magento\Directory\Model\ CurrencyFactory $currencyFactory
46
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
47
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $config
57
+ * @param Context $context
58
+ * @param CurrencyFactory $currencyFactory
59
+ * @param StoreManagerInterface $storeManager
60
+ * @param ScopeConfigInterface $config
48
61
* @param string $connectionName
49
62
*/
50
63
public function __construct (
51
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ Context $ context ,
52
- \ Magento \ Directory \ Model \ CurrencyFactory $ currencyFactory ,
53
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
54
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ config ,
64
+ Context $ context ,
65
+ CurrencyFactory $ currencyFactory ,
66
+ StoreManagerInterface $ storeManager ,
67
+ ScopeConfigInterface $ config ,
55
68
$ connectionName = null
56
69
) {
57
70
$ this ->_currencyFactory = $ currencyFactory ;
@@ -74,10 +87,10 @@ protected function _construct()
74
87
* Proceed operations after object is saved
75
88
* Save options store data
76
89
*
77
- * @param \Magento\Framework\Model\ AbstractModel $object
78
- * @return \Magento\Framework\Model\ResourceModel\Db\ AbstractDb
90
+ * @param AbstractModel $object
91
+ * @return AbstractDb
79
92
*/
80
- protected function _afterSave (\ Magento \ Framework \ Model \ AbstractModel $ object )
93
+ protected function _afterSave (AbstractModel $ object )
81
94
{
82
95
$ this ->_saveValuePrices ($ object );
83
96
$ this ->_saveValueTitles ($ object );
@@ -88,20 +101,21 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
88
101
/**
89
102
* Save option value price data
90
103
*
91
- * @param \Magento\Framework\Model\ AbstractModel $object
104
+ * @param AbstractModel $object
92
105
* @return void
93
106
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
94
107
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
95
108
*/
96
- protected function _saveValuePrices (\ Magento \ Framework \ Model \ AbstractModel $ object )
109
+ protected function _saveValuePrices (AbstractModel $ object )
97
110
{
111
+ $ objectPrice = $ object ->getPrice ();
98
112
$ priceTable = $ this ->getTable ('catalog_product_option_type_price ' );
99
- $ formattedPrice = $ this ->getLocaleFormatter ()->getNumber ($ object -> getPrice () );
113
+ $ formattedPrice = $ this ->getLocaleFormatter ()->getNumber ($ objectPrice );
100
114
101
115
$ price = (double )sprintf ('%F ' , $ formattedPrice );
102
116
$ priceType = $ object ->getPriceType ();
103
117
104
- if ($ object -> getPrice ( ) && $ priceType ) {
118
+ if (isset ( $ objectPrice ) && $ priceType ) {
105
119
//save for store_id = 0
106
120
$ select = $ this ->getConnection ()->select ()->from (
107
121
$ priceTable ,
@@ -111,7 +125,7 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
111
125
(int )$ object ->getId ()
112
126
)->where (
113
127
'store_id = ? ' ,
114
- \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID
128
+ Store::DEFAULT_STORE_ID
115
129
);
116
130
$ optionTypeId = $ this ->getConnection ()->fetchOne ($ select );
117
131
@@ -120,15 +134,15 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
120
134
$ bind = ['price ' => $ price , 'price_type ' => $ priceType ];
121
135
$ where = [
122
136
'option_type_id = ? ' => $ optionTypeId ,
123
- 'store_id = ? ' => \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID ,
137
+ 'store_id = ? ' => Store::DEFAULT_STORE_ID ,
124
138
];
125
139
126
140
$ this ->getConnection ()->update ($ priceTable , $ bind , $ where );
127
141
}
128
142
} else {
129
143
$ bind = [
130
144
'option_type_id ' => (int )$ object ->getId (),
131
- 'store_id ' => \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID ,
145
+ 'store_id ' => Store::DEFAULT_STORE_ID ,
132
146
'price ' => $ price ,
133
147
'price_type ' => $ priceType ,
134
148
];
@@ -137,17 +151,17 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
137
151
}
138
152
139
153
$ scope = (int )$ this ->_config ->getValue (
140
- \ Magento \ Store \ Model \ Store::XML_PATH_PRICE_SCOPE ,
141
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
154
+ Store::XML_PATH_PRICE_SCOPE ,
155
+ ScopeInterface::SCOPE_STORE
142
156
);
143
157
144
- if ($ scope == \ Magento \ Store \ Model \ Store::PRICE_SCOPE_WEBSITE
158
+ if ($ scope == Store::PRICE_SCOPE_WEBSITE
145
159
&& $ priceType
146
- && $ object -> getPrice ( )
147
- && $ object ->getStoreId () != \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID
160
+ && isset ( $ objectPrice )
161
+ && $ object ->getStoreId () != Store::DEFAULT_STORE_ID
148
162
) {
149
163
$ baseCurrency = $ this ->_config ->getValue (
150
- \ Magento \ Directory \ Model \ Currency::XML_PATH_CURRENCY_BASE ,
164
+ Currency::XML_PATH_CURRENCY_BASE ,
151
165
'default '
152
166
);
153
167
@@ -156,7 +170,7 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
156
170
foreach ($ storeIds as $ storeId ) {
157
171
if ($ priceType == 'fixed ' ) {
158
172
$ storeCurrency = $ this ->_storeManager ->getStore ($ storeId )->getBaseCurrencyCode ();
159
- /** @var $currencyModel \Magento\Directory\Model\ Currency */
173
+ /** @var $currencyModel Currency */
160
174
$ currencyModel = $ this ->_currencyFactory ->create ();
161
175
$ currencyModel ->load ($ baseCurrency );
162
176
$ rate = $ currencyModel ->getRate ($ storeCurrency );
@@ -198,8 +212,8 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
198
212
}
199
213
}
200
214
} else {
201
- if ($ scope == \ Magento \ Store \ Model \ Store::PRICE_SCOPE_WEBSITE
202
- && !$ object -> getPrice ( )
215
+ if ($ scope == Store::PRICE_SCOPE_WEBSITE
216
+ && !isset ( $ objectPrice )
203
217
&& !$ priceType
204
218
) {
205
219
$ storeIds = $ this ->_storeManager ->getStore ($ object ->getStoreId ())->getWebsite ()->getStoreIds ();
@@ -217,13 +231,13 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
217
231
/**
218
232
* Save option value title data
219
233
*
220
- * @param \Magento\Framework\Model\ AbstractModel $object
234
+ * @param AbstractModel $object
221
235
* @return void
222
236
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
223
237
*/
224
- protected function _saveValueTitles (\ Magento \ Framework \ Model \ AbstractModel $ object )
238
+ protected function _saveValueTitles (AbstractModel $ object )
225
239
{
226
- foreach ([\ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID , $ object ->getStoreId ()] as $ storeId ) {
240
+ foreach ([Store::DEFAULT_STORE_ID , $ object ->getStoreId ()] as $ storeId ) {
227
241
$ titleTable = $ this ->getTable ('catalog_product_option_type_title ' );
228
242
$ select = $ this ->getConnection ()->select ()->from (
229
243
$ titleTable ,
@@ -238,7 +252,7 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
238
252
$ optionTypeId = $ this ->getConnection ()->fetchOne ($ select );
239
253
$ existInCurrentStore = $ this ->getOptionIdFromOptionTable ($ titleTable , (int )$ object ->getId (), (int )$ storeId );
240
254
241
- if ($ storeId != \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID && $ object ->getData ('is_delete_store_title ' )) {
255
+ if ($ storeId != Store::DEFAULT_STORE_ID && $ object ->getData ('is_delete_store_title ' )) {
242
256
$ object ->unsetData ('title ' );
243
257
}
244
258
@@ -256,11 +270,11 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
256
270
$ existInDefaultStore = $ this ->getOptionIdFromOptionTable (
257
271
$ titleTable ,
258
272
(int )$ object ->getId (),
259
- \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID
273
+ Store::DEFAULT_STORE_ID
260
274
);
261
275
// we should insert record into not default store only of if it does not exist in default store
262
- if (($ storeId == \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID && !$ existInDefaultStore )
263
- || ($ storeId != \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID && !$ existInCurrentStore )
276
+ if (($ storeId == Store::DEFAULT_STORE_ID && !$ existInDefaultStore )
277
+ || ($ storeId != Store::DEFAULT_STORE_ID && !$ existInCurrentStore )
264
278
) {
265
279
$ bind = [
266
280
'option_type_id ' => (int )$ object ->getId (),
@@ -273,7 +287,7 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
273
287
} else {
274
288
if ($ storeId
275
289
&& $ optionTypeId
276
- && $ object ->getStoreId () > \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID
290
+ && $ object ->getStoreId () > Store::DEFAULT_STORE_ID
277
291
) {
278
292
$ where = [
279
293
'option_type_id = ? ' => (int )$ optionTypeId ,
@@ -353,12 +367,12 @@ public function deleteValues($optionTypeId)
353
367
/**
354
368
* Duplicate product options value
355
369
*
356
- * @param \Magento\Catalog\Model\Product\Option\Value $object
370
+ * @param OptionValue $object
357
371
* @param int $oldOptionId
358
372
* @param int $newOptionId
359
- * @return \Magento\Catalog\Model\Product\Option\Value
373
+ * @return OptionValue
360
374
*/
361
- public function duplicate (\ Magento \ Catalog \ Model \ Product \ Option \ Value $ object , $ oldOptionId , $ newOptionId )
375
+ public function duplicate (OptionValue $ object , $ oldOptionId , $ newOptionId )
362
376
{
363
377
$ connection = $ this ->getConnection ();
364
378
$ select = $ connection ->select ()->from ($ this ->getMainTable ())->where ('option_id = ? ' , $ oldOptionId );
@@ -425,14 +439,14 @@ public function duplicate(\Magento\Catalog\Model\Product\Option\Value $object, $
425
439
/**
426
440
* Get FormatInterface to convert price from string to number format
427
441
*
428
- * @return \Magento\Framework\Locale\ FormatInterface
442
+ * @return FormatInterface
429
443
* @deprecated 101.0.8
430
444
*/
431
445
private function getLocaleFormatter ()
432
446
{
433
447
if ($ this ->localeFormat === null ) {
434
- $ this ->localeFormat = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
435
- ->get (\ Magento \ Framework \ Locale \ FormatInterface::class);
448
+ $ this ->localeFormat = ObjectManager::getInstance ()
449
+ ->get (FormatInterface::class);
436
450
}
437
451
return $ this ->localeFormat ;
438
452
}
0 commit comments