@@ -56,6 +56,11 @@ class Collection extends \Magento\Catalog\Model\Resource\Product\Collection
56
56
*/
57
57
protected $ _productType ;
58
58
59
+ /**
60
+ * @var \Magento\Quote\Model\Resource\Quote\Collection
61
+ */
62
+ protected $ quoteResource ;
63
+
59
64
/**
60
65
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
61
66
* @param \Psr\Log\LoggerInterface $logger
@@ -79,6 +84,7 @@ class Collection extends \Magento\Catalog\Model\Resource\Product\Collection
79
84
* @param \Magento\Catalog\Model\Resource\Product $product
80
85
* @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory
81
86
* @param \Magento\Catalog\Model\Product\Type $productType
87
+ * @param \Magento\Quote\Model\Resource\Quote\Collection $quoteResource
82
88
* @param mixed $connection
83
89
*
84
90
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -106,6 +112,7 @@ public function __construct(
106
112
\Magento \Catalog \Model \Resource \Product $ product ,
107
113
\Magento \Reports \Model \Event \TypeFactory $ eventTypeFactory ,
108
114
\Magento \Catalog \Model \Product \Type $ productType ,
115
+ \Magento \Quote \Model \Resource \Quote \Collection $ quoteResource ,
109
116
$ connection = null
110
117
) {
111
118
$ this ->setProductEntityId ($ product ->getEntityIdField ());
@@ -135,6 +142,7 @@ public function __construct(
135
142
);
136
143
$ this ->_eventTypeFactory = $ eventTypeFactory ;
137
144
$ this ->_productType = $ productType ;
145
+ $ this ->quoteResource = $ quoteResource ;
138
146
}
139
147
140
148
/**
@@ -239,7 +247,7 @@ protected function _joinFields()
239
247
public function getSelectCountSql ()
240
248
{
241
249
if ($ this ->_selectCountSqlType == self ::SELECT_COUNT_SQL_TYPE_CART ) {
242
- $ countSelect = clone $ this ->getSelect ();
250
+ $ countSelect = clone $ this ->quoteResource -> getSelect ();
243
251
$ countSelect ->reset ()->from (
244
252
['quote_item_table ' => $ this ->getTable ('quote_item ' )],
245
253
['COUNT(DISTINCT quote_item_table.product_id) ' ]
@@ -263,102 +271,6 @@ public function getSelectCountSql()
263
271
return $ countSelect ;
264
272
}
265
273
266
- /**
267
- * Add orders count
268
- *
269
- * @param string $from
270
- * @param string $to
271
- * @return $this
272
- */
273
- public function addOrdersCount ($ from = '' , $ to = '' )
274
- {
275
- $ orderItemTableName = $ this ->getTable ('sales_order_item ' );
276
- $ productFieldName = sprintf ('e.%s ' , $ this ->getProductEntityId ());
277
-
278
- $ this ->getSelect ()->joinLeft (
279
- ['order_items ' => $ orderItemTableName ],
280
- "order_items.product_id = {$ productFieldName }" ,
281
- []
282
- )->columns (
283
- ['orders ' => 'COUNT(order_items2.item_id) ' ]
284
- )->group (
285
- $ productFieldName
286
- );
287
-
288
- $ dateFilter = ['order_items2.item_id = order_items.item_id ' ];
289
- if ($ from != '' && $ to != '' ) {
290
- $ dateFilter [] = $ this ->_prepareBetweenSql ('order_items2.created_at ' , $ from , $ to );
291
- }
292
-
293
- $ this ->getSelect ()->joinLeft (
294
- ['order_items2 ' => $ orderItemTableName ],
295
- implode (' AND ' , $ dateFilter ),
296
- []
297
- );
298
-
299
- return $ this ;
300
- }
301
-
302
- /**
303
- * Add ordered qty's
304
- *
305
- * @param string $from
306
- * @param string $to
307
- * @return $this
308
- */
309
- public function addOrderedQty ($ from = '' , $ to = '' )
310
- {
311
- $ adapter = $ this ->getConnection ();
312
- $ compositeTypeIds = $ this ->_productType ->getCompositeTypes ();
313
- $ orderTableAliasName = $ adapter ->quoteIdentifier ('order ' );
314
-
315
- $ orderJoinCondition = [
316
- $ orderTableAliasName . '.entity_id = order_items.order_id ' ,
317
- $ adapter ->quoteInto ("{$ orderTableAliasName }.state <> ? " , \Magento \Sales \Model \Order::STATE_CANCELED ),
318
- ];
319
-
320
- $ productJoinCondition = [
321
- $ adapter ->quoteInto ('(e.type_id NOT IN (?)) ' , $ compositeTypeIds ),
322
- 'e.entity_id = order_items.product_id ' ,
323
- $ adapter ->quoteInto ('e.attribute_set_id = ? ' , $ this ->getProductAttributeSetId ()),
324
- ];
325
-
326
- if ($ from != '' && $ to != '' ) {
327
- $ fieldName = $ orderTableAliasName . '.created_at ' ;
328
- $ orderJoinCondition [] = $ this ->_prepareBetweenSql ($ fieldName , $ from , $ to );
329
- }
330
-
331
- $ this ->getSelect ()->reset ()->from (
332
- ['order_items ' => $ this ->getTable ('sales_order_item ' )],
333
- ['ordered_qty ' => 'SUM(order_items.qty_ordered) ' , 'order_items_name ' => 'order_items.name ' ]
334
- )->joinInner (
335
- ['order ' => $ this ->getTable ('sales_order ' )],
336
- implode (' AND ' , $ orderJoinCondition ),
337
- []
338
- )->joinLeft (
339
- ['e ' => $ this ->getProductEntityTableName ()],
340
- implode (' AND ' , $ productJoinCondition ),
341
- [
342
- 'entity_id ' => 'order_items.product_id ' ,
343
- 'attribute_set_id ' => 'e.attribute_set_id ' ,
344
- 'type_id ' => 'e.type_id ' ,
345
- 'sku ' => 'e.sku ' ,
346
- 'has_options ' => 'e.has_options ' ,
347
- 'required_options ' => 'e.required_options ' ,
348
- 'created_at ' => 'e.created_at ' ,
349
- 'updated_at ' => 'e.updated_at '
350
- ]
351
- )->where (
352
- 'parent_item_id IS NULL '
353
- )->group (
354
- 'order_items.product_id '
355
- )->having (
356
- 'SUM(order_items.qty_ordered) > ? ' ,
357
- 0
358
- );
359
- return $ this ;
360
- }
361
-
362
274
/**
363
275
* Set order
364
276
*
@@ -368,7 +280,7 @@ public function addOrderedQty($from = '', $to = '')
368
280
*/
369
281
public function setOrder ($ attribute , $ dir = self ::SORT_ORDER_DESC )
370
282
{
371
- if (in_array ($ attribute , ['carts ' , ' orders ' , ' ordered_qty ' ])) {
283
+ if (in_array ($ attribute , ['carts ' ])) {
372
284
$ this ->getSelect ()->order ($ attribute . ' ' . $ dir );
373
285
} else {
374
286
parent ::setOrder ($ attribute , $ dir );
0 commit comments