@@ -153,52 +153,43 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null)
153
153
$ specialPrice = $ this ->_addAttributeToSelect ($ select , 'special_price ' , "e. $ linkField " , 'cs.store_id ' );
154
154
$ specialFrom = $ this ->_addAttributeToSelect ($ select , 'special_from_date ' , "e. $ linkField " , 'cs.store_id ' );
155
155
$ specialTo = $ this ->_addAttributeToSelect ($ select , 'special_to_date ' , "e. $ linkField " , 'cs.store_id ' );
156
- $ curentDate = new \Zend_Db_Expr ('cwd.website_date ' );
157
-
158
- $ specialExpr = $ connection ->getCheckSql (
159
- $ connection ->getCheckSql (
160
- $ specialFrom . ' IS NULL ' ,
161
- '1 ' ,
162
- $ connection ->getCheckSql ($ specialFrom . ' <= ' . $ curentDate , '1 ' , '0 ' )
163
- ) . " > 0 AND " . $ connection ->getCheckSql (
164
- $ specialTo . ' IS NULL ' ,
165
- '1 ' ,
166
- $ connection ->getCheckSql ($ specialTo . ' >= ' . $ curentDate , '1 ' , '0 ' )
167
- ) . " > 0 AND {$ specialPrice } > 0 AND {$ specialPrice } < 100 " ,
168
- $ specialPrice ,
169
- '0 '
170
- );
156
+ $ currentDate = new \Zend_Db_Expr ('cwd.website_date ' );
171
157
172
- $ tierExpr = new \Zend_Db_Expr ("tp.min_price " );
158
+ $ specialFromDate = $ connection ->getDatePartSql ($ specialFrom );
159
+ $ specialToDate = $ connection ->getDatePartSql ($ specialTo );
160
+ $ specialFromExpr = "{$ specialFrom } IS NULL OR {$ specialFromDate } <= {$ currentDate }" ;
161
+ $ specialToExpr = "{$ specialTo } IS NULL OR {$ specialToDate } >= {$ currentDate }" ;
162
+ $ specialExpr = "{$ specialPrice } IS NOT NULL AND {$ specialPrice } > 0 AND {$ specialPrice } < 100 "
163
+ . " AND {$ specialFromExpr } AND {$ specialToExpr }" ;
164
+ $ tierExpr = new \Zend_Db_Expr ('tp.min_price ' );
173
165
174
166
if ($ priceType == \Magento \Bundle \Model \Product \Price::PRICE_TYPE_FIXED ) {
175
- $ finalPrice = $ connection ->getCheckSql (
176
- $ specialExpr . ' > 0 ' ,
177
- 'ROUND( ' . $ price . ' * ( ' . $ specialExpr . ' / 100), 4) ' ,
178
- $ price
167
+ $ specialPriceExpr = $ connection ->getCheckSql (
168
+ $ specialExpr ,
169
+ 'ROUND( ' . $ price . ' * ( ' . $ specialPrice . ' / 100), 4) ' ,
170
+ ' NULL '
179
171
);
180
172
$ tierPrice = $ connection ->getCheckSql (
181
173
$ tierExpr . ' IS NOT NULL ' ,
182
- 'ROUND( ' . $ price . ' - ' . ' ( ' . $ price . ' * ( ' . $ tierExpr . ' / 100)) , 4) ' ,
174
+ 'ROUND((1 - ' . $ tierExpr . ' / 100) * ' . $ price . ', 4) ' ,
183
175
'NULL '
184
176
);
185
-
186
- $ finalPrice = $ connection ->getCheckSql (
187
- "{$ tierPrice } < {$ finalPrice }" ,
188
- $ tierPrice ,
189
- $ finalPrice
190
- );
177
+ $ finalPrice = $ connection ->getLeastSql ([
178
+ $ price ,
179
+ $ connection ->getIfNullSql ($ specialPriceExpr , $ price ),
180
+ $ connection ->getIfNullSql ($ tierPrice , $ price ),
181
+ ]);
191
182
} else {
192
- $ finalPrice = new \Zend_Db_Expr (" 0 " );
183
+ $ finalPrice = new \Zend_Db_Expr (' 0 ' );
193
184
$ tierPrice = $ connection ->getCheckSql ($ tierExpr . ' IS NOT NULL ' , '0 ' , 'NULL ' );
194
185
}
195
186
196
187
$ select ->columns (
197
188
[
198
189
'price_type ' => new \Zend_Db_Expr ($ priceType ),
199
- 'special_price ' => $ specialExpr ,
190
+ 'special_price ' => $ connection -> getCheckSql ( $ specialExpr, $ specialPrice , ' 0 ' ) ,
200
191
'tier_percent ' => $ tierExpr ,
201
- 'orig_price ' => $ connection ->getCheckSql ($ price . ' IS NULL ' , '0 ' , $ price ),
192
+ 'orig_price ' => $ connection ->getIfNullSql ($ price , '0 ' ),
202
193
'price ' => $ finalPrice ,
203
194
'min_price ' => $ finalPrice ,
204
195
'max_price ' => $ finalPrice ,
@@ -246,63 +237,29 @@ protected function _calculateBundleOptionPrice()
246
237
$ this ->_prepareBundleOptionTable ();
247
238
248
239
$ select = $ connection ->select ()->from (
249
- [ ' i ' => $ this ->_getBundleSelectionTable ()] ,
240
+ $ this ->_getBundleSelectionTable (),
250
241
['entity_id ' , 'customer_group_id ' , 'website_id ' , 'option_id ' ]
251
242
)->group (
252
- ['entity_id ' , 'customer_group_id ' , 'website_id ' , 'option_id ' , 'is_required ' , 'group_type ' ]
253
- )->columns (
243
+ ['entity_id ' , 'customer_group_id ' , 'website_id ' , 'option_id ' ]
244
+ );
245
+ $ minPrice = $ connection ->getCheckSql ('is_required = 1 ' , 'price ' , 'NULL ' );
246
+ $ tierPrice = $ connection ->getCheckSql ('is_required = 1 ' , 'tier_price ' , 'NULL ' );
247
+ $ select ->columns (
254
248
[
255
- 'min_price ' => $ connection -> getCheckSql ( ' i.is_required = 1 ' , ' MIN(i.price) ' , ' 0 ' ),
256
- 'alt_price ' => $ connection -> getCheckSql ( ' i.is_required = 0 ' , ' MIN(i. price) ' , ' 0 ' ),
257
- 'max_price ' => $ connection ->getCheckSql ('i. group_type = 1 ' , 'SUM(i. price) ' , 'MAX(i. price) ' ),
258
- 'tier_price ' => $ connection -> getCheckSql ( ' i.is_required = 1 ' , ' MIN(i.tier_price) ' , ' 0 ' ),
259
- 'alt_tier_price ' => $ connection -> getCheckSql ( ' i.is_required = 0 ' , ' MIN(i. tier_price) ' , ' 0 ' ),
249
+ 'min_price ' => new \ Zend_Db_Expr ( ' MIN( ' . $ minPrice . ' ) ' ),
250
+ 'alt_price ' => new \ Zend_Db_Expr ( ' MIN(price) ' ),
251
+ 'max_price ' => $ connection ->getCheckSql ('group_type = 0 ' , 'MAX( price) ' , 'SUM( price) ' ),
252
+ 'tier_price ' => new \ Zend_Db_Expr ( ' MIN( ' . $ tierPrice . ' ) ' ),
253
+ 'alt_tier_price ' => new \ Zend_Db_Expr ( ' MIN(tier_price) ' ),
260
254
]
261
255
);
262
256
263
257
$ query = $ select ->insertFromSelect ($ this ->_getBundleOptionTable ());
264
258
$ connection ->query ($ query );
265
259
266
260
$ this ->_prepareDefaultFinalPriceTable ();
267
-
268
- $ minPrice = new \Zend_Db_Expr (
269
- $ connection ->getCheckSql ('SUM(io.min_price) = 0 ' , 'MIN(io.alt_price) ' , 'SUM(io.min_price) ' ) . ' + i.price '
270
- );
271
- $ maxPrice = new \Zend_Db_Expr ("SUM(io.max_price) + i.price " );
272
- $ tierPrice = $ connection ->getCheckSql (
273
- 'MIN(i.tier_percent) IS NOT NULL ' ,
274
- $ connection ->getCheckSql (
275
- 'SUM(io.tier_price) = 0 ' ,
276
- 'SUM(io.alt_tier_price) ' ,
277
- 'SUM(io.tier_price) '
278
- ) . ' + MIN(i.tier_price) ' ,
279
- 'NULL '
280
- );
281
-
282
- $ select = $ connection ->select ()->from (
283
- ['io ' => $ this ->_getBundleOptionTable ()],
284
- ['entity_id ' , 'customer_group_id ' , 'website_id ' ]
285
- )->join (
286
- ['i ' => $ this ->_getBundlePriceTable ()],
287
- 'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id ' .
288
- ' AND i.website_id = io.website_id ' ,
289
- []
290
- )->group (
291
- ['io.entity_id ' , 'io.customer_group_id ' , 'io.website_id ' , 'i.tax_class_id ' , 'i.orig_price ' , 'i.price ' ]
292
- )->columns (
293
- [
294
- 'i.tax_class_id ' ,
295
- 'orig_price ' => 'i.orig_price ' ,
296
- 'price ' => 'i.price ' ,
297
- 'min_price ' => $ minPrice ,
298
- 'max_price ' => $ maxPrice ,
299
- 'tier_price ' => $ tierPrice ,
300
- 'base_tier ' => 'MIN(i.base_tier) ' ,
301
- ]
302
- );
303
-
304
- $ query = $ select ->insertFromSelect ($ this ->_getDefaultFinalPriceTable ());
305
- $ connection ->query ($ query );
261
+ $ this ->applyBundlePrice ();
262
+ $ this ->applyBundleOptionPrice ();
306
263
307
264
return $ this ;
308
265
}
@@ -348,33 +305,33 @@ protected function _calculateBundleSelectionPrice($priceType)
348
305
'ROUND(i.base_tier - (i.base_tier * ( ' . $ selectionPriceValue . ' / 100)),4) ' ,
349
306
$ connection ->getCheckSql (
350
307
'i.tier_percent > 0 ' ,
351
- 'ROUND( ' .
352
- $ selectionPriceValue .
353
- ' - ( ' .
354
- $ selectionPriceValue .
355
- ' * (i.tier_percent / 100)),4) ' ,
308
+ 'ROUND((1 - i.tier_percent / 100) * ' . $ selectionPriceValue . ',4) ' ,
356
309
$ selectionPriceValue
357
310
)
358
311
) . ' * bs.selection_qty ' ,
359
312
'NULL '
360
313
);
361
314
362
- $ priceExpr = new \Zend_Db_Expr (
363
- $ connection ->getCheckSql ("{$ tierExpr } < {$ priceExpr }" , $ tierExpr , $ priceExpr )
364
- );
315
+ $ priceExpr = $ connection ->getLeastSql ([
316
+ $ priceExpr ,
317
+ $ connection ->getIfNullSql ($ tierExpr , $ priceExpr ),
318
+ ]);
365
319
} else {
366
- $ priceExpr = new \Zend_Db_Expr (
367
- $ connection ->getCheckSql (
368
- 'i.special_price > 0 AND i.special_price < 100 ' ,
369
- 'ROUND(idx.min_price * (i.special_price / 100), 4) ' ,
370
- 'idx.min_price '
371
- ) . ' * bs.selection_qty '
320
+ $ price = 'idx.min_price * bs.selection_qty ' ;
321
+ $ specialExpr = $ connection ->getCheckSql (
322
+ 'i.special_price > 0 AND i.special_price < 100 ' ,
323
+ 'ROUND( ' . $ price . ' * (i.special_price / 100), 4) ' ,
324
+ $ price
372
325
);
373
326
$ tierExpr = $ connection ->getCheckSql (
374
- 'i.base_tier IS NOT NULL ' ,
375
- 'ROUND(idx.min_price * (i.base_tier / 100), 4)* bs.selection_qty ' ,
327
+ 'i.tier_percent IS NOT NULL ' ,
328
+ 'ROUND((1 - i.tier_percent / 100) * ' . $ price . ' , 4) ' ,
376
329
'NULL '
377
330
);
331
+ $ priceExpr = $ connection ->getLeastSql ([
332
+ $ specialExpr ,
333
+ $ connection ->getIfNullSql ($ tierExpr , $ price ),
334
+ ]);
378
335
}
379
336
380
337
$ linkField = $ this ->getMetadataPool ()->getMetadata (ProductInterface::class)->getLinkField ();
@@ -508,4 +465,76 @@ protected function _prepareTierPriceIndex($entityIds = null)
508
465
509
466
return $ this ;
510
467
}
468
+
469
+ /**
470
+ * Create bundle price.
471
+ *
472
+ * @return void
473
+ */
474
+ private function applyBundlePrice () : void
475
+ {
476
+ $ select = $ this ->getConnection ()->select ();
477
+ $ select ->from (
478
+ $ this ->_getBundlePriceTable (),
479
+ [
480
+ 'entity_id ' ,
481
+ 'customer_group_id ' ,
482
+ 'website_id ' ,
483
+ 'tax_class_id ' ,
484
+ 'orig_price ' ,
485
+ 'price ' ,
486
+ 'min_price ' ,
487
+ 'max_price ' ,
488
+ 'tier_price ' ,
489
+ 'base_tier ' ,
490
+ ]
491
+ );
492
+
493
+ $ query = $ select ->insertFromSelect ($ this ->_getDefaultFinalPriceTable ());
494
+ $ this ->getConnection ()->query ($ query );
495
+ }
496
+
497
+ /**
498
+ * Make insert/update bundle option price.
499
+ *
500
+ * @return void
501
+ */
502
+ private function applyBundleOptionPrice () : void
503
+ {
504
+ $ connection = $ this ->getConnection ();
505
+
506
+ $ subSelect = $ connection ->select ()->from (
507
+ $ this ->_getBundleOptionTable (),
508
+ [
509
+ 'entity_id ' ,
510
+ 'customer_group_id ' ,
511
+ 'website_id ' ,
512
+ 'min_price ' => new \Zend_Db_Expr ('SUM(min_price) ' ),
513
+ 'alt_price ' => new \Zend_Db_Expr ('MIN(alt_price) ' ),
514
+ 'max_price ' => new \Zend_Db_Expr ('SUM(max_price) ' ),
515
+ 'tier_price ' => new \Zend_Db_Expr ('SUM(tier_price) ' ),
516
+ 'alt_tier_price ' => new \Zend_Db_Expr ('MIN(alt_tier_price) ' ),
517
+ ]
518
+ )->group (
519
+ ['entity_id ' , 'customer_group_id ' , 'website_id ' ]
520
+ );
521
+
522
+ $ minPrice = 'i.min_price + ' . $ connection ->getIfNullSql ('io.min_price ' , '0 ' );
523
+ $ tierPrice = 'i.tier_price + ' . $ connection ->getIfNullSql ('io.tier_price ' , '0 ' );
524
+ $ select = $ connection ->select ()->join (
525
+ ['io ' => $ subSelect ],
526
+ 'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id ' .
527
+ ' AND i.website_id = io.website_id ' ,
528
+ []
529
+ )->columns (
530
+ [
531
+ 'min_price ' => $ connection ->getCheckSql ("{$ minPrice } = 0 " , 'io.alt_price ' , $ minPrice ),
532
+ 'max_price ' => new \Zend_Db_Expr ('io.max_price + i.max_price ' ),
533
+ 'tier_price ' => $ connection ->getCheckSql ("{$ tierPrice } = 0 " , 'io.alt_tier_price ' , $ tierPrice ),
534
+ ]
535
+ );
536
+
537
+ $ query = $ select ->crossUpdateFromSelect (['i ' => $ this ->_getDefaultFinalPriceTable ()]);
538
+ $ connection ->query ($ query );
539
+ }
511
540
}
0 commit comments