@@ -52,6 +52,16 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
52
52
*/
53
53
private $ hasEntity = null ;
54
54
55
+ /**
56
+ * @var IndexTableStructureFactory
57
+ */
58
+ private $ indexTableStructureFactory ;
59
+
60
+ /**
61
+ * @var PriceModifierInterface[]
62
+ */
63
+ private $ priceModifiers = [];
64
+
55
65
/**
56
66
* DefaultPrice constructor.
57
67
*
@@ -61,19 +71,34 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
61
71
* @param \Magento\Framework\Event\ManagerInterface $eventManager
62
72
* @param \Magento\Framework\Module\Manager $moduleManager
63
73
* @param string|null $connectionName
64
- * @param null|\Magento\Indexer\Model\Indexer\StateFactory $stateFactory
74
+ * @param null|IndexTableStructureFactory $indexTableStructureFactory
75
+ * @param PriceModifierInterface[] $priceModifiers
65
76
*/
66
77
public function __construct (
67
78
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
68
79
\Magento \Framework \Indexer \Table \StrategyInterface $ tableStrategy ,
69
80
\Magento \Eav \Model \Config $ eavConfig ,
70
81
\Magento \Framework \Event \ManagerInterface $ eventManager ,
71
82
\Magento \Framework \Module \Manager $ moduleManager ,
72
- $ connectionName = null
83
+ $ connectionName = null ,
84
+ IndexTableStructureFactory $ indexTableStructureFactory = null ,
85
+ array $ priceModifiers = []
73
86
) {
74
87
$ this ->_eventManager = $ eventManager ;
75
88
$ this ->moduleManager = $ moduleManager ;
76
89
parent ::__construct ($ context , $ tableStrategy , $ eavConfig , $ connectionName );
90
+
91
+ $ this ->indexTableStructureFactory = $ indexTableStructureFactory ?:
92
+ \Magento \Framework \App \ObjectManager::getInstance ()->get (IndexTableStructureFactory::class);
93
+ foreach ($ priceModifiers as $ priceModifier ) {
94
+ if (!($ priceModifier instanceof PriceModifierInterface)) {
95
+ throw new \InvalidArgumentException (
96
+ 'Argument \'priceModifiers \' must be of the type ' . PriceModifierInterface::class . '[] '
97
+ );
98
+ }
99
+
100
+ $ this ->priceModifiers [] = $ priceModifier ;
101
+ }
77
102
}
78
103
79
104
/**
@@ -209,13 +234,41 @@ protected function _getDefaultFinalPriceTable()
209
234
* Prepare final price temporary index table
210
235
*
211
236
* @return $this
237
+ * @deprecated
238
+ * @see prepareFinalPriceTable()
212
239
*/
213
240
protected function _prepareDefaultFinalPriceTable ()
214
241
{
215
242
$ this ->getConnection ()->delete ($ this ->_getDefaultFinalPriceTable ());
216
243
return $ this ;
217
244
}
218
245
246
+ /**
247
+ * Create (if needed), clean and return structure of final price table
248
+ *
249
+ * @return IndexTableStructure
250
+ */
251
+ private function prepareFinalPriceTable ()
252
+ {
253
+ $ tableName = $ this ->_getDefaultFinalPriceTable ();
254
+ $ this ->getConnection ()->delete ($ tableName );
255
+
256
+ $ finalPriceTable = $ this ->indexTableStructureFactory ->create ([
257
+ 'tableName ' => $ tableName ,
258
+ 'entityField ' => 'entity_id ' ,
259
+ 'customerGroupField ' => 'customer_group_id ' ,
260
+ 'websiteField ' => 'website_id ' ,
261
+ 'taxClassField ' => 'tax_class_id ' ,
262
+ 'originalPriceField ' => 'orig_price ' ,
263
+ 'finalPriceField ' => 'price ' ,
264
+ 'minPriceField ' => 'min_price ' ,
265
+ 'maxPriceField ' => 'max_price ' ,
266
+ 'tierPriceField ' => 'tier_price ' ,
267
+ ]);
268
+
269
+ return $ finalPriceTable ;
270
+ }
271
+
219
272
/**
220
273
* Retrieve website current dates table name
221
274
*
@@ -248,11 +301,14 @@ protected function _prepareFinalPriceData($entityIds = null)
248
301
*/
249
302
protected function prepareFinalPriceDataForType ($ entityIds , $ type )
250
303
{
251
- $ this ->_prepareDefaultFinalPriceTable ();
304
+ $ finalPriceTable = $ this ->prepareFinalPriceTable ();
252
305
253
306
$ select = $ this ->getSelect ($ entityIds , $ type );
254
- $ query = $ select ->insertFromSelect ($ this -> _getDefaultFinalPriceTable (), [], false );
307
+ $ query = $ select ->insertFromSelect ($ finalPriceTable -> getTableName (), [], false );
255
308
$ this ->getConnection ()->query ($ query );
309
+
310
+ $ this ->applyDiscountPrices ($ finalPriceTable );
311
+
256
312
return $ this ;
257
313
}
258
314
@@ -359,7 +415,7 @@ protected function getSelect($entityIds = null, $type = null)
359
415
'e. ' . $ metadata ->getLinkField (),
360
416
'cs.store_id '
361
417
);
362
- $ currentDate = $ connection -> getDatePartSql ( 'cwd.website_date ' ) ;
418
+ $ currentDate = 'cwd.website_date ' ;
363
419
364
420
$ maxUnsignedBigint = '~0 ' ;
365
421
$ specialFromDate = $ connection ->getDatePartSql ($ specialFrom );
@@ -409,6 +465,7 @@ protected function getSelect($entityIds = null, $type = null)
409
465
'store_field ' => new \Zend_Db_Expr ('cs.store_id ' ),
410
466
]
411
467
);
468
+
412
469
return $ select ;
413
470
}
414
471
@@ -454,6 +511,19 @@ protected function _prepareCustomOptionPriceTable()
454
511
return $ this ;
455
512
}
456
513
514
+ /**
515
+ * Apply discount prices to final price index table.
516
+ *
517
+ * @param IndexTableStructure $finalPriceTable
518
+ * @return void
519
+ */
520
+ private function applyDiscountPrices (IndexTableStructure $ finalPriceTable ) : void
521
+ {
522
+ foreach ($ this ->priceModifiers as $ priceModifier ) {
523
+ $ priceModifier ->modifyPrice ($ finalPriceTable );
524
+ }
525
+ }
526
+
457
527
/**
458
528
* Apply custom option minimal and maximal price to temporary final price index table
459
529
*
@@ -463,14 +533,15 @@ protected function _prepareCustomOptionPriceTable()
463
533
protected function _applyCustomOption ()
464
534
{
465
535
$ connection = $ this ->getConnection ();
536
+ $ finalPriceTable = $ this ->_getDefaultFinalPriceTable ();
466
537
$ coaTable = $ this ->_getCustomOptionAggregateTable ();
467
538
$ copTable = $ this ->_getCustomOptionPriceTable ();
468
539
469
540
$ this ->_prepareCustomOptionAggregateTable ();
470
541
$ this ->_prepareCustomOptionPriceTable ();
471
542
472
543
$ select = $ connection ->select ()->from (
473
- ['i ' => $ this -> _getDefaultFinalPriceTable () ],
544
+ ['i ' => $ finalPriceTable ],
474
545
['entity_id ' , 'customer_group_id ' , 'website_id ' ]
475
546
)->join (
476
547
['cw ' => $ this ->getTable ('store_website ' )],
@@ -537,7 +608,7 @@ protected function _applyCustomOption()
537
608
$ connection ->query ($ query );
538
609
539
610
$ select = $ connection ->select ()->from (
540
- ['i ' => $ this -> _getDefaultFinalPriceTable () ],
611
+ ['i ' => $ finalPriceTable ],
541
612
['entity_id ' , 'customer_group_id ' , 'website_id ' ]
542
613
)->join (
543
614
['cw ' => $ this ->getTable ('store_website ' )],
@@ -606,7 +677,7 @@ protected function _applyCustomOption()
606
677
$ query = $ select ->insertFromSelect ($ copTable );
607
678
$ connection ->query ($ query );
608
679
609
- $ table = ['i ' => $ this -> _getDefaultFinalPriceTable () ];
680
+ $ table = ['i ' => $ finalPriceTable ];
610
681
$ select = $ connection ->select ()->join (
611
682
['io ' => $ copTable ],
612
683
'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id ' .
0 commit comments