@@ -39,12 +39,17 @@ class Collection extends \Magento\Quote\Model\Resource\Quote\Collection
39
39
/**
40
40
* @var \Magento\Catalog\Model\Resource\Product\Collection
41
41
*/
42
- protected $ _productResource ;
42
+ protected $ productResource ;
43
43
44
44
/**
45
45
* @var \Magento\Customer\Model\Resource\Customer
46
46
*/
47
- protected $ _customerResource ;
47
+ protected $ customerResource ;
48
+
49
+ /**
50
+ * @var \Magento\Sales\Model\Resource\Order\Collection
51
+ */
52
+ protected $ orderResource ;
48
53
49
54
/**
50
55
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
@@ -53,6 +58,7 @@ class Collection extends \Magento\Quote\Model\Resource\Quote\Collection
53
58
* @param \Magento\Framework\Event\ManagerInterface $eventManager
54
59
* @param \Magento\Catalog\Model\Resource\Product\Collection $productResource
55
60
* @param \Magento\Customer\Model\Resource\Customer $customerResource
61
+ * @param \Magento\Sales\Model\Resource\Order\Collection $orderResource
56
62
* @param null $connection
57
63
* @param \Magento\Framework\Model\Resource\Db\AbstractDb $resource
58
64
*/
@@ -63,12 +69,14 @@ public function __construct(
63
69
\Magento \Framework \Event \ManagerInterface $ eventManager ,
64
70
\Magento \Catalog \Model \Resource \Product \Collection $ productResource ,
65
71
\Magento \Customer \Model \Resource \Customer $ customerResource ,
72
+ \Magento \Sales \Model \Resource \Order \Collection $ orderResource ,
66
73
$ connection = null ,
67
74
\Magento \Framework \Model \Resource \Db \AbstractDb $ resource = null
68
75
) {
69
76
parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
70
- $ this ->_productResource = $ productResource ;
71
- $ this ->_customerResource = $ customerResource ;
77
+ $ this ->productResource = $ productResource ;
78
+ $ this ->customerResource = $ customerResource ;
79
+ $ this ->orderResource = $ orderResource ;
72
80
}
73
81
74
82
/**
@@ -145,21 +153,22 @@ public function prepareActiveCartItems()
145
153
}
146
154
147
155
/**
148
- * Orders quantity subselect
156
+ * Orders quantity data
149
157
*
150
- * @return \Magento\Framework\DB\Select
158
+ * @param array $productIds
159
+ * @return array
151
160
*/
152
- protected function getOrdersSubSelect ( )
161
+ protected function getOrdersData ( array $ productIds )
153
162
{
154
- $ ordersSubSelect = clone $ this ->getSelect ();
163
+ $ ordersSubSelect = clone $ this ->orderResource -> getSelect ();
155
164
$ ordersSubSelect ->reset ()->from (
156
165
['oi ' => $ this ->getTable ('sales_order_item ' )],
157
166
['orders ' => new \Zend_Db_Expr ('COUNT(1) ' ), 'product_id ' ]
158
- )->group (
167
+ )->where ( ' oi.product_id IN (?) ' , $ productIds )-> group (
159
168
'oi.product_id '
160
169
);
161
170
162
- return $ ordersSubSelect ;
171
+ return $ this -> orderResource -> getConnection ()-> fetchAssoc ( $ ordersSubSelect) ;
163
172
}
164
173
165
174
/**
@@ -182,7 +191,7 @@ public function addStoreFilter($storeIds)
182
191
*/
183
192
public function addCustomerData ($ filter = null )
184
193
{
185
- $ customersSelect = $ this ->_customerResource ->getReadConnection ()->select ();
194
+ $ customersSelect = $ this ->customerResource ->getReadConnection ()->select ();
186
195
$ customersSelect ->from (['customer ' => 'customer_entity ' ], 'entity_id ' );
187
196
if (isset ($ filter ['customer_name ' ])) {
188
197
$ customersSelect = $ this ->getCustomerNames ($ customersSelect );
@@ -194,7 +203,7 @@ public function addCustomerData($filter = null)
194
203
if (isset ($ filter ['email ' ])) {
195
204
$ customersSelect ->where ('customer.email LIKE ? ' , '% ' . $ filter ['email ' ] . '% ' );
196
205
}
197
- $ filteredCustomers = $ this ->_customerResource ->getReadConnection ()->fetchCol ($ customersSelect );
206
+ $ filteredCustomers = $ this ->customerResource ->getReadConnection ()->fetchCol ($ customersSelect );
198
207
$ this ->getSelect ()->where ('main_table.customer_id IN (?) ' , $ filteredCustomers );
199
208
return $ this ;
200
209
}
@@ -258,10 +267,10 @@ public function getSelectCountSql()
258
267
*/
259
268
protected function getCustomerNames ($ select )
260
269
{
261
- $ attrFirstname = $ this ->_customerResource ->getAttribute ('firstname ' );
270
+ $ attrFirstname = $ this ->customerResource ->getAttribute ('firstname ' );
262
271
$ attrFirstnameId = (int )$ attrFirstname ->getAttributeId ();
263
272
$ attrFirstnameTableName = $ attrFirstname ->getBackend ()->getTable ();
264
- $ attrLastname = $ this ->_customerResource ->getAttribute ('lastname ' );
273
+ $ attrLastname = $ this ->customerResource ->getAttribute ('lastname ' );
265
274
$ attrLastnameId = (int )$ attrLastname ->getAttributeId ();
266
275
$ attrLastnameTableName = $ attrLastname ->getBackend ()->getTable ();
267
276
$ select ->joinInner (
@@ -287,7 +296,7 @@ protected function getCustomerNames($select)
287
296
*/
288
297
public function resolveCustomerNames ()
289
298
{
290
- $ select = $ this ->_customerResource ->getReadConnection ()->select ();
299
+ $ select = $ this ->customerResource ->getReadConnection ()->select ();
291
300
$ customerName = $ select ->getAdapter ()->getConcatSql (['cust_fname.value ' , 'cust_lname.value ' ], ' ' );
292
301
293
302
$ select ->from (
@@ -313,13 +322,13 @@ public function resolveCustomerNames()
313
322
*/
314
323
protected function getProductData ()
315
324
{
316
- $ productConnection = $ this ->_productResource ->getConnection ('read ' );
317
- $ productAttrName = $ this ->_productResource ->getAttribute ('name ' );
325
+ $ productConnection = $ this ->productResource ->getConnection ('read ' );
326
+ $ productAttrName = $ this ->productResource ->getAttribute ('name ' );
318
327
$ productAttrNameId = (int )$ productAttrName ->getAttributeId ();
319
- $ productAttrPrice = $ this ->_productResource ->getAttribute ('price ' );
328
+ $ productAttrPrice = $ this ->productResource ->getAttribute ('price ' );
320
329
$ productAttrPriceId = (int )$ productAttrPrice ->getAttributeId ();
321
330
322
- $ select = clone $ this ->_productResource ->getSelect ();
331
+ $ select = clone $ this ->productResource ->getSelect ();
323
332
$ select ->reset ();
324
333
$ select ->from (
325
334
['main_table ' => $ this ->getTable ('catalog_product_entity ' )]
@@ -335,14 +344,7 @@ protected function getProductData()
335
344
['product_price ' => $ productAttrPrice ->getBackend ()->getTable ()],
336
345
"product_price.entity_id = main_table.entity_id AND product_price.attribute_id = {$ productAttrPriceId }" ,
337
346
['price ' => new \Zend_Db_Expr ('product_price.value ' )]
338
- )->joinLeft (
339
- ['order_items ' => new \Zend_Db_Expr (sprintf ('(%s) ' , $ this ->getOrdersSubSelect ()))],
340
- 'order_items.product_id = main_table.entity_id ' ,
341
- []
342
- )->columns (
343
- 'order_items.orders '
344
347
);
345
-
346
348
$ productData = $ productConnection ->fetchAssoc ($ select );
347
349
return $ productData ;
348
350
}
@@ -356,12 +358,14 @@ protected function _afterLoad()
356
358
{
357
359
parent ::_afterLoad ();
358
360
$ productData = $ this ->getProductData ();
361
+ $ productIds = array_keys ($ productData );
362
+ $ orderData = $ this ->getOrdersData ($ productIds );
359
363
$ items = $ this ->getItems ();
360
364
foreach ($ items as $ item ) {
361
365
$ item ->setId ($ item ->getProductId ());
362
366
$ item ->setPrice ($ productData [$ item ->getProductId ()]['price ' ] * $ item ->getBaseToGlobalRate ());
363
367
$ item ->setName ($ productData [$ item ->getProductId ()]['name ' ]);
364
- $ item ->setOrders ($ productData [$ item ->getProductId ()]['orders ' ]);
368
+ $ item ->setOrders ($ orderData [$ item ->getProductId ()]['orders ' ]);
365
369
}
366
370
367
371
return $ this ;
0 commit comments