@@ -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
/**
@@ -120,75 +128,47 @@ public function prepareForAbandonedReport($storeIds, $filter = null)
120
128
/**
121
129
* Prepare select query for products in carts report
122
130
*
123
- * @return $this
131
+ * @return \Magento\Framework\DB\Select
124
132
*/
125
- public function prepareForProductsInCarts ()
133
+ public function prepareActiveCartItems ()
126
134
{
127
- $ productAttrName = $ this ->_productResource ->getAttribute ('name ' );
128
- $ productAttrNameId = (int )$ productAttrName ->getAttributeId ();
129
- $ productAttrNameTable = $ productAttrName ->getBackend ()->getTable ();
130
- $ productAttrPrice = $ this ->_productResource ->getAttribute ('price ' );
131
- $ productAttrPriceId = (int )$ productAttrPrice ->getAttributeId ();
132
- $ productAttrPriceTable = $ productAttrPrice ->getBackend ()->getTable ();
133
-
134
- $ this ->getSelect ()->useStraightJoin (
135
- true
136
- )->reset (
137
- \Zend_Db_Select::COLUMNS
138
- )->joinInner (
139
- ['quote_items ' => $ this ->getTable ('quote_item ' )],
140
- 'quote_items.quote_id = main_table.entity_id ' ,
141
- null
142
- )->joinInner (
143
- ['e ' => $ this ->getTable ('catalog_product_entity ' )],
144
- 'e.entity_id = quote_items.product_id ' ,
145
- null
146
- )->joinInner (
147
- ['product_name ' => $ productAttrNameTable ],
148
- 'product_name.entity_id = e.entity_id '
149
- . ' AND product_name.attribute_id = ' . $ productAttrNameId
150
- . ' AND product_name.store_id = ' . \Magento \Store \Model \Store::DEFAULT_STORE_ID ,
151
- ['name ' => 'product_name.value ' ]
152
- )->joinInner (
153
- ['product_price ' => $ productAttrPriceTable ],
154
- "product_price.entity_id = e.entity_id AND product_price.attribute_id = {$ productAttrPriceId }" ,
155
- ['price ' => new \Zend_Db_Expr ('product_price.value * main_table.base_to_global_rate ' )]
156
- )->joinLeft (
157
- ['order_items ' => new \Zend_Db_Expr (sprintf ('(%s) ' , $ this ->getOrdersSubSelect ()))],
158
- 'order_items.product_id = e.entity_id ' ,
159
- []
160
- )->columns (
161
- 'e.* '
162
- )->columns (
163
- ['carts ' => new \Zend_Db_Expr ('COUNT(quote_items.item_id) ' )]
164
- )->columns (
165
- 'order_items.orders '
166
- )->where (
167
- 'main_table.is_active = ? ' ,
168
- 1
169
- )->group (
170
- 'quote_items.product_id '
171
- );
135
+ $ quoteItemsSelect = $ this ->getSelect ();
136
+ $ quoteItemsSelect ->reset ()
137
+ ->from (['main_table ' => $ this ->getTable ('quote ' )], '' )
138
+ ->columns ('quote_items.product_id ' )
139
+ ->columns (['carts ' => new \Zend_Db_Expr ('COUNT(quote_items.item_id) ' )])
140
+ ->columns ('main_table.base_to_global_rate ' )
141
+ ->joinInner (
142
+ ['quote_items ' => $ this ->getTable ('quote_item ' )],
143
+ 'quote_items.quote_id = main_table.entity_id ' ,
144
+ null
145
+ )->where (
146
+ 'main_table.is_active = ? ' ,
147
+ 1
148
+ )->group (
149
+ 'quote_items.product_id '
150
+ );
172
151
173
- return $ this ;
152
+ return $ quoteItemsSelect ;
174
153
}
175
154
176
155
/**
177
- * Orders quantity subselect
156
+ * Orders quantity data
178
157
*
179
- * @return \Magento\Framework\DB\Select
158
+ * @param array $productIds
159
+ * @return array
180
160
*/
181
- protected function getOrdersSubSelect ( )
161
+ protected function getOrdersData ( array $ productIds )
182
162
{
183
- $ ordersSubSelect = clone $ this ->getSelect ();
163
+ $ ordersSubSelect = clone $ this ->orderResource -> getSelect ();
184
164
$ ordersSubSelect ->reset ()->from (
185
165
['oi ' => $ this ->getTable ('sales_order_item ' )],
186
166
['orders ' => new \Zend_Db_Expr ('COUNT(1) ' ), 'product_id ' ]
187
- )->group (
167
+ )->where ( ' oi.product_id IN (?) ' , $ productIds )-> group (
188
168
'oi.product_id '
189
169
);
190
170
191
- return $ ordersSubSelect ;
171
+ return $ this -> orderResource -> getConnection ()-> fetchAssoc ( $ ordersSubSelect) ;
192
172
}
193
173
194
174
/**
@@ -211,7 +191,7 @@ public function addStoreFilter($storeIds)
211
191
*/
212
192
public function addCustomerData ($ filter = null )
213
193
{
214
- $ customersSelect = $ this ->_customerResource ->getReadConnection ()->select ();
194
+ $ customersSelect = $ this ->customerResource ->getReadConnection ()->select ();
215
195
$ customersSelect ->from (['customer ' => 'customer_entity ' ], 'entity_id ' );
216
196
if (isset ($ filter ['customer_name ' ])) {
217
197
$ customersSelect = $ this ->getCustomerNames ($ customersSelect );
@@ -223,7 +203,7 @@ public function addCustomerData($filter = null)
223
203
if (isset ($ filter ['email ' ])) {
224
204
$ customersSelect ->where ('customer.email LIKE ? ' , '% ' . $ filter ['email ' ] . '% ' );
225
205
}
226
- $ filteredCustomers = $ this ->_customerResource ->getReadConnection ()->fetchCol ($ customersSelect );
206
+ $ filteredCustomers = $ this ->customerResource ->getReadConnection ()->fetchCol ($ customersSelect );
227
207
$ this ->getSelect ()->where ('main_table.customer_id IN (?) ' , $ filteredCustomers );
228
208
return $ this ;
229
209
}
@@ -270,24 +250,14 @@ public function addSubtotal($storeIds = '', $filter = null)
270
250
/**
271
251
* Get select count sql
272
252
*
273
- * @return string
253
+ * @return \Magento\Framework\DB\Select
274
254
*/
275
255
public function getSelectCountSql ()
276
256
{
277
- $ countSelect = clone $ this ->getSelect ();
278
- $ countSelect ->reset (\Zend_Db_Select::ORDER );
279
- $ countSelect ->reset (\Zend_Db_Select::LIMIT_COUNT );
280
- $ countSelect ->reset (\Zend_Db_Select::LIMIT_OFFSET );
281
- $ countSelect ->reset (\Zend_Db_Select::COLUMNS );
282
- $ countSelect ->reset (\Zend_Db_Select::GROUP );
283
- $ countSelect ->resetJoinLeft ();
284
-
285
- if ($ this ->_selectCountSqlType == self ::SELECT_COUNT_SQL_TYPE_CART ) {
286
- $ countSelect ->columns ("COUNT(DISTINCT e.entity_id) " );
287
- } else {
288
- $ countSelect ->columns ("COUNT(DISTINCT main_table.entity_id) " );
289
- }
290
-
257
+ $ countSelect = clone $ this ->prepareActiveCartItems ();
258
+ $ countSelect ->reset (\Zend_Db_Select::COLUMNS )
259
+ ->reset (\Zend_Db_Select::GROUP )
260
+ ->columns ('COUNT(DISTINCT quote_items.product_id) ' );
291
261
return $ countSelect ;
292
262
}
293
263
@@ -297,10 +267,10 @@ public function getSelectCountSql()
297
267
*/
298
268
protected function getCustomerNames ($ select )
299
269
{
300
- $ attrFirstname = $ this ->_customerResource ->getAttribute ('firstname ' );
270
+ $ attrFirstname = $ this ->customerResource ->getAttribute ('firstname ' );
301
271
$ attrFirstnameId = (int )$ attrFirstname ->getAttributeId ();
302
272
$ attrFirstnameTableName = $ attrFirstname ->getBackend ()->getTable ();
303
- $ attrLastname = $ this ->_customerResource ->getAttribute ('lastname ' );
273
+ $ attrLastname = $ this ->customerResource ->getAttribute ('lastname ' );
304
274
$ attrLastnameId = (int )$ attrLastname ->getAttributeId ();
305
275
$ attrLastnameTableName = $ attrLastname ->getBackend ()->getTable ();
306
276
$ select ->joinInner (
@@ -326,7 +296,7 @@ protected function getCustomerNames($select)
326
296
*/
327
297
public function resolveCustomerNames ()
328
298
{
329
- $ select = $ this ->_customerResource ->getReadConnection ()->select ();
299
+ $ select = $ this ->customerResource ->getReadConnection ()->select ();
330
300
$ customerName = $ select ->getAdapter ()->getConcatSql (['cust_fname.value ' , 'cust_lname.value ' ], ' ' );
331
301
332
302
$ select ->from (
@@ -343,4 +313,61 @@ public function resolveCustomerNames()
343
313
next ($ customersData );
344
314
}
345
315
}
316
+
317
+ /**
318
+ * Separate query for product and order data
319
+ *
320
+ * @return array
321
+ * @throws \Magento\Eav\Exception
322
+ */
323
+ protected function getProductData ()
324
+ {
325
+ $ productConnection = $ this ->productResource ->getConnection ('read ' );
326
+ $ productAttrName = $ this ->productResource ->getAttribute ('name ' );
327
+ $ productAttrNameId = (int )$ productAttrName ->getAttributeId ();
328
+ $ productAttrPrice = $ this ->productResource ->getAttribute ('price ' );
329
+ $ productAttrPriceId = (int )$ productAttrPrice ->getAttributeId ();
330
+
331
+ $ select = clone $ this ->productResource ->getSelect ();
332
+ $ select ->reset ();
333
+ $ select ->from (
334
+ ['main_table ' => $ this ->getTable ('catalog_product_entity ' )]
335
+ )->useStraightJoin (
336
+ true
337
+ )->joinInner (
338
+ ['product_name ' => $ productAttrName ->getBackend ()->getTable ()],
339
+ 'product_name.entity_id = main_table.entity_id '
340
+ . ' AND product_name.attribute_id = ' . $ productAttrNameId
341
+ . ' AND product_name.store_id = ' . \Magento \Store \Model \Store::DEFAULT_STORE_ID ,
342
+ ['name ' => 'product_name.value ' ]
343
+ )->joinInner (
344
+ ['product_price ' => $ productAttrPrice ->getBackend ()->getTable ()],
345
+ "product_price.entity_id = main_table.entity_id AND product_price.attribute_id = {$ productAttrPriceId }" ,
346
+ ['price ' => new \Zend_Db_Expr ('product_price.value ' )]
347
+ );
348
+ $ productData = $ productConnection ->fetchAssoc ($ select );
349
+ return $ productData ;
350
+ }
351
+
352
+ /**
353
+ * Add data fetched from another database
354
+ *
355
+ * @return $this
356
+ */
357
+ protected function _afterLoad ()
358
+ {
359
+ parent ::_afterLoad ();
360
+ $ productData = $ this ->getProductData ();
361
+ $ productIds = array_keys ($ productData );
362
+ $ orderData = $ this ->getOrdersData ($ productIds );
363
+ $ items = $ this ->getItems ();
364
+ foreach ($ items as $ item ) {
365
+ $ item ->setId ($ item ->getProductId ());
366
+ $ item ->setPrice ($ productData [$ item ->getProductId ()]['price ' ] * $ item ->getBaseToGlobalRate ());
367
+ $ item ->setName ($ productData [$ item ->getProductId ()]['name ' ]);
368
+ $ item ->setOrders ($ orderData [$ item ->getProductId ()]['orders ' ]);
369
+ }
370
+
371
+ return $ this ;
372
+ }
346
373
}
0 commit comments