16
16
use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \IndexTableStructureFactory ;
17
17
use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \IndexTableStructure ;
18
18
use Magento \GroupedProduct \Model \ResourceModel \Product \Link ;
19
+ use Magento \GroupedProduct \Model \Product \Type \Grouped as GroupedType ;
19
20
20
21
/**
21
22
* Calculate minimal and maximal prices for Grouped products
@@ -111,24 +112,43 @@ public function executeByDimension(array $dimensions, \Traversable $entityIds =
111
112
/**
112
113
* Prepare data index select for Grouped products prices
113
114
* @param $dimensions
114
- * @param array $entityIds the parent entity ids limitation
115
+ * @param int| array $entityIds the parent entity ids limitation
115
116
* @return \Magento\Framework\DB\Select
116
117
*/
117
118
protected function _prepareGroupedProductPriceDataSelect ($ dimensions , $ entityIds = null )
118
119
{
119
- $ connection = $ this ->getConnection ();
120
+ $ select = $ this ->getConnection ()-> select ();
120
121
121
- $ taxClassId = $ connection -> getCheckSql ( ' MIN(i.tax_class_id) IS NULL ' , ' 0 ' , ' MIN(i.tax_class_id) ' );
122
- $ minCheckSql = $ connection -> getCheckSql ( ' e.required_options = 0 ' , ' i.min_price ' , 0 );
123
- $ maxCheckSql = $ connection -> getCheckSql ( ' e.required_options = 0 ' , ' i.max_price ' , 0 );
124
- $ linkField = $ this -> metadataPool -> getMetadata (ProductInterface::class)-> getLinkField ( );
122
+ $ select -> from (
123
+ [ ' e ' => $ this -> getTable ( ' catalog_product_entity ' )],
124
+ ' entity_id '
125
+ );
125
126
126
- $ select = $ connection ->select ()->from (['l ' => $ this ->getTable ('catalog_product_link ' )], []);
127
+ $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
128
+ $ select ->joinLeft (
129
+ ['l ' => $ this ->getTable ('catalog_product_link ' )],
130
+ 'e. ' . $ linkField . ' = l.product_id AND l.link_type_id= ' . Link::LINK_TYPE_GROUPED ,
131
+ []
132
+ );
133
+ //aditional infromation about inner products
134
+ $ select ->joinLeft (
135
+ ['le ' => $ this ->getTable ('catalog_product_entity ' )],
136
+ 'le.entity_id = l.linked_product_id ' ,
137
+ []
138
+ );
127
139
$ select ->columns (
128
140
[
129
- 'entity_id ' => new \Zend_Db_Expr ('l.product_id ' ),
130
- 'customer_group_id ' => new \Zend_Db_Expr ('i.customer_group_id ' ),
131
- 'website_id ' => new \Zend_Db_Expr ('i.website_id ' ),
141
+ 'i.customer_group_id ' ,
142
+ 'i.website_id ' ,
143
+ ]
144
+ );
145
+ $ taxClassId = $ this ->getConnection ()->getCheckSql ('MIN(i.tax_class_id) IS NULL ' , '0 ' , 'MIN(i.tax_class_id) ' );
146
+ $ minCheckSql = $ this ->getConnection ()->getCheckSql ('le.required_options = 0 ' , 'i.min_price ' , 0 );
147
+ $ maxCheckSql = $ this ->getConnection ()->getCheckSql ('le.required_options = 0 ' , 'i.max_price ' , 0 );
148
+ $ select ->joinLeft (
149
+ ['i ' => $ this ->getMainTable ($ dimensions )],
150
+ 'i.entity_id = l.linked_product_id ' ,
151
+ [
132
152
'tax_class_id ' => $ taxClassId ,
133
153
'price ' => new \Zend_Db_Expr ('NULL ' ),
134
154
'final_price ' => new \Zend_Db_Expr ('NULL ' ),
@@ -137,13 +157,16 @@ protected function _prepareGroupedProductPriceDataSelect($dimensions, $entityIds
137
157
'tier_price ' => new \Zend_Db_Expr ('NULL ' ),
138
158
]
139
159
);
140
- $ select ->join (['i ' => $ this ->getMainTable ($ dimensions )], 'i.entity_id = l.linked_product_id ' , []);
141
- $ select ->join (['e ' => $ this ->getTable ('catalog_product_entity ' )], "e. $ linkField = l.linked_product_id " , []);
142
- $ select ->where ('l.link_type_id = ? ' , Link::LINK_TYPE_GROUPED );
143
- $ select ->group (['l.product_id ' , 'i.customer_group_id ' , 'i.website_id ' ]);
160
+ $ select ->group (
161
+ ['e.entity_id ' , 'i.customer_group_id ' , 'i.website_id ' ]
162
+ );
163
+ $ select ->where (
164
+ 'e.type_id=? ' ,
165
+ GroupedType::TYPE_CODE
166
+ );
144
167
145
168
if ($ entityIds !== null ) {
146
- $ select ->where ('l.product_id IN(?) ' , $ entityIds );
169
+ $ select ->where ('e.entity_id IN(?) ' , $ entityIds );
147
170
}
148
171
149
172
return $ select ;
0 commit comments