6
6
namespace Magento \Catalog \Model \ResourceModel \Product ;
7
7
8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \Framework \DataObject ;
10
+ use Magento \Framework \Model \AbstractModel ;
9
11
use Magento \Store \Model \Store ;
10
12
11
13
/**
@@ -77,10 +79,10 @@ protected function _construct()
77
79
/**
78
80
* Save options store data
79
81
*
80
- * @param \Magento\Framework\Model\ AbstractModel $object
82
+ * @param AbstractModel $object
81
83
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
82
84
*/
83
- protected function _afterSave (\ Magento \ Framework \ Model \ AbstractModel $ object )
85
+ protected function _afterSave (AbstractModel $ object )
84
86
{
85
87
$ this ->_saveValuePrices ($ object );
86
88
$ this ->_saveValueTitles ($ object );
@@ -91,66 +93,21 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
91
93
/**
92
94
* Save value prices
93
95
*
94
- * @param \Magento\Framework\Model\ AbstractModel $object
96
+ * @param AbstractModel $object
95
97
* @return $this
96
98
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
97
99
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
98
100
*/
99
- protected function _saveValuePrices (\ Magento \ Framework \ Model \ AbstractModel $ object )
101
+ protected function _saveValuePrices (AbstractModel $ object )
100
102
{
101
- $ priceTable = $ this ->getTable ('catalog_product_option_price ' );
102
- $ connection = $ this ->getConnection ();
103
-
104
103
/*
105
104
* Better to check param 'price' and 'price_type' for saving.
106
105
* If there is not price skip saving price
107
106
*/
108
-
109
107
if (in_array ($ object ->getType (), $ this ->getPriceTypes ())) {
110
108
// save for store_id = 0
111
109
if (!$ object ->getData ('scope ' , 'price ' )) {
112
- $ statement = $ connection ->select ()->from (
113
- $ priceTable ,
114
- 'option_id '
115
- )->where (
116
- 'option_id = ? ' ,
117
- $ object ->getId ()
118
- )->where (
119
- 'store_id = ? ' ,
120
- Store::DEFAULT_STORE_ID
121
- );
122
- $ optionId = $ connection ->fetchOne ($ statement );
123
-
124
- if (!$ optionId ) {
125
- $ data = $ this ->_prepareDataForTable (
126
- new \Magento \Framework \DataObject (
127
- [
128
- 'option_id ' => $ object ->getId (),
129
- 'store_id ' => Store::DEFAULT_STORE_ID ,
130
- 'price ' => $ object ->getPrice (),
131
- 'price_type ' => $ object ->getPriceType (),
132
- ]
133
- ),
134
- $ priceTable
135
- );
136
- $ connection ->insert ($ priceTable , $ data );
137
- } elseif ((int )$ object ->getStoreId () === Store::DEFAULT_STORE_ID ) {
138
- $ data = $ this ->_prepareDataForTable (
139
- new \Magento \Framework \DataObject (
140
- ['price ' => $ object ->getPrice (), 'price_type ' => $ object ->getPriceType ()]
141
- ),
142
- $ priceTable
143
- );
144
-
145
- $ connection ->update (
146
- $ priceTable ,
147
- $ data ,
148
- [
149
- 'option_id = ? ' => $ object ->getId (),
150
- 'store_id = ? ' => Store::DEFAULT_STORE_ID
151
- ]
152
- );
153
- }
110
+ $ this ->savePriceByStore ($ object , Store::DEFAULT_STORE_ID );
154
111
}
155
112
156
113
$ scope = (int )$ this ->_config ->getValue (
@@ -178,49 +135,12 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
178
135
$ newPrice = $ object ->getPrice ();
179
136
}
180
137
181
- $ statement = $ connection ->select ()->from (
182
- $ priceTable
183
- )->where (
184
- 'option_id = ? ' ,
185
- $ object ->getId ()
186
- )->where (
187
- 'store_id = ? ' ,
188
- $ storeId
189
- );
190
-
191
- if ($ connection ->fetchOne ($ statement )) {
192
- $ data = $ this ->_prepareDataForTable (
193
- new \Magento \Framework \DataObject (
194
- ['price ' => $ newPrice , 'price_type ' => $ object ->getPriceType ()]
195
- ),
196
- $ priceTable
197
- );
198
-
199
- $ connection ->update (
200
- $ priceTable ,
201
- $ data ,
202
- ['option_id = ? ' => $ object ->getId (), 'store_id = ? ' => $ storeId ]
203
- );
204
- } else {
205
- $ data = $ this ->_prepareDataForTable (
206
- new \Magento \Framework \DataObject (
207
- [
208
- 'option_id ' => $ object ->getId (),
209
- 'store_id ' => $ storeId ,
210
- 'price ' => $ newPrice ,
211
- 'price_type ' => $ object ->getPriceType (),
212
- ]
213
- ),
214
- $ priceTable
215
- );
216
- $ connection ->insert ($ priceTable , $ data );
217
- }
138
+ $ this ->savePriceByStore ($ object , (int )$ storeId , $ newPrice );
218
139
}
219
140
}
220
- } elseif ($ scope == Store::PRICE_SCOPE_WEBSITE && $ object ->getData ('scope ' , 'price ' )
221
- ) {
222
- $ connection ->delete (
223
- $ priceTable ,
141
+ } elseif ($ scope == Store::PRICE_SCOPE_WEBSITE && $ object ->getData ('scope ' , 'price ' )) {
142
+ $ this ->getConnection ()->delete (
143
+ $ this ->getTable ('catalog_product_option_price ' ),
224
144
['option_id = ? ' => $ object ->getId (), 'store_id = ? ' => $ object ->getStoreId ()]
225
145
);
226
146
}
@@ -229,14 +149,68 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
229
149
return $ this ;
230
150
}
231
151
152
+ /**
153
+ * Save option price by store
154
+ *
155
+ * @param AbstractModel $object
156
+ * @param int $storeId
157
+ * @param float|null $newPrice
158
+ */
159
+ private function savePriceByStore (AbstractModel $ object , int $ storeId , float $ newPrice = null ): void
160
+ {
161
+ $ priceTable = $ this ->getTable ('catalog_product_option_price ' );
162
+ $ connection = $ this ->getConnection ();
163
+ $ price = $ newPrice === null ? $ object ->getPrice () : $ newPrice ;
164
+
165
+ $ statement = $ connection ->select ()->from ($ priceTable , 'option_id ' )
166
+ ->where ('option_id = ? ' , $ object ->getId ())
167
+ ->where ('store_id = ? ' , $ storeId );
168
+ $ optionId = $ connection ->fetchOne ($ statement );
169
+
170
+ if (!$ optionId ) {
171
+ $ data = $ this ->_prepareDataForTable (
172
+ new DataObject ([
173
+ 'option_id ' => $ object ->getId (),
174
+ 'store_id ' => $ storeId ,
175
+ 'price ' => $ price ,
176
+ 'price_type ' => $ object ->getPriceType (),
177
+ ]),
178
+ $ priceTable
179
+ );
180
+ $ connection ->insert ($ priceTable , $ data );
181
+ } else {
182
+ // skip to update the default price when the store price is saving
183
+ if ($ storeId === Store::DEFAULT_STORE_ID && (int )$ object ->getStoreId () !== $ storeId ) {
184
+ return ;
185
+ }
186
+
187
+ $ data = $ this ->_prepareDataForTable (
188
+ new DataObject ([
189
+ 'price ' => $ price ,
190
+ 'price_type ' => $ object ->getPriceType ()
191
+ ]),
192
+ $ priceTable
193
+ );
194
+
195
+ $ connection ->update (
196
+ $ priceTable ,
197
+ $ data ,
198
+ [
199
+ 'option_id = ? ' => $ object ->getId (),
200
+ 'store_id = ? ' => $ storeId
201
+ ]
202
+ );
203
+ }
204
+ }
205
+
232
206
/**
233
207
* Save titles
234
208
*
235
- * @param \Magento\Framework\Model\ AbstractModel $object
209
+ * @param AbstractModel $object
236
210
* @return void
237
211
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
238
212
*/
239
- protected function _saveValueTitles (\ Magento \ Framework \ Model \ AbstractModel $ object )
213
+ protected function _saveValueTitles (AbstractModel $ object )
240
214
{
241
215
$ connection = $ this ->getConnection ();
242
216
$ titleTableName = $ this ->getTable ('catalog_product_option_title ' );
@@ -583,6 +557,8 @@ public function getPriceTypes()
583
557
}
584
558
585
559
/**
560
+ * Get Metadata Pool
561
+ *
586
562
* @return \Magento\Framework\EntityManager\MetadataPool
587
563
*/
588
564
private function getMetadataPool ()
0 commit comments