@@ -167,7 +167,7 @@ protected function getOrdersData(array $productIds)
167
167
$ ordersSubSelect = clone $ this ->orderResource ->getSelect ();
168
168
$ ordersSubSelect ->reset ()->from (
169
169
['oi ' => $ this ->getTable ('sales_order_item ' )],
170
- ['orders ' => new \Zend_Db_Expr ('COUNT(1) ' ), ' product_id ' ]
170
+ ['product_id ' , ' orders ' => new \Zend_Db_Expr ('COUNT(1) ' )]
171
171
)->where ('oi.product_id IN (?) ' , $ productIds )->group (
172
172
'oi.product_id '
173
173
);
@@ -321,10 +321,11 @@ public function resolveCustomerNames()
321
321
/**
322
322
* Separate query for product and order data
323
323
*
324
+ * @param array $productIds
324
325
* @return array
325
326
* @throws \Magento\Eav\Exception
326
327
*/
327
- protected function getProductData ()
328
+ protected function getProductData (array $ productIds )
328
329
{
329
330
$ productConnection = $ this ->productResource ->getConnection ('read ' );
330
331
$ productAttrName = $ this ->productResource ->getAttribute ('name ' );
@@ -348,7 +349,8 @@ protected function getProductData()
348
349
['product_price ' => $ productAttrPrice ->getBackend ()->getTable ()],
349
350
"product_price.entity_id = main_table.entity_id AND product_price.attribute_id = {$ productAttrPriceId }" ,
350
351
['price ' => new \Zend_Db_Expr ('product_price.value ' )]
351
- );
352
+ )->where ('main_table.entity_id IN (?) ' , $ productIds );
353
+
352
354
$ productData = $ productConnection ->fetchAssoc ($ select );
353
355
return $ productData ;
354
356
}
@@ -361,10 +363,13 @@ protected function getProductData()
361
363
protected function _afterLoad ()
362
364
{
363
365
parent ::_afterLoad ();
364
- $ productData = $ this ->getProductData ();
365
- $ productIds = array_keys ($ productData );
366
- $ orderData = $ this ->getOrdersData ($ productIds );
367
366
$ items = $ this ->getItems ();
367
+ $ productIds = [];
368
+ foreach ($ items as $ item ) {
369
+ $ productIds [] = $ item ->getProductId ();
370
+ }
371
+ $ productData = $ this ->getProductData ($ productIds );
372
+ $ orderData = $ this ->getOrdersData ($ productIds );
368
373
foreach ($ items as $ item ) {
369
374
$ item ->setId ($ item ->getProductId ());
370
375
$ item ->setPrice ($ productData [$ item ->getProductId ()]['price ' ] * $ item ->getBaseToGlobalRate ());
0 commit comments