1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2011 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
namespace Magento \Catalog \Model \ResourceModel \Category ;
7
7
@@ -337,36 +337,7 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
337
337
$ categoryIds = array_keys ($ anchor );
338
338
$ countSelect = $ this ->getProductsCountQuery ($ categoryIds , (bool )$ websiteId );
339
339
$ categoryProductsCount = $ this ->_conn ->fetchPairs ($ countSelect );
340
-
341
- $ subSelect = clone $ this ->_conn ->select ();
342
- $ subSelect ->from (['ce2 ' => $ this ->getTable ('catalog_category_entity ' )], 'ce2.entity_id ' )
343
- ->where ("ce2.path LIKE CONCAT(ce.path, '/%') " );
344
-
345
- $ select = clone $ this ->_conn ->select ();
346
- $ select ->from (['ce ' => $ this ->getTable (
347
- 'catalog_category_entity ' )],
348
- 'ce.entity_id '
349
- );
350
- $ joinCondition = new \Zend_Db_Expr ("ce.entity_id=cp.category_id OR cp.category_id IN ( {$ subSelect }) " );
351
- $ select ->joinLeft ([
352
- 'cp ' => $ this ->getProductTable ()],
353
- $ joinCondition ,
354
- 'COUNT(DISTINCT cp.product_id) AS product_count '
355
- );
356
- if ($ websiteId ) {
357
- $ select ->join (
358
- ['w ' => $ this ->getProductWebsiteTable ()],
359
- 'cp.product_id = w.product_id ' ,
360
- []
361
- )->where (
362
- 'w.website_id = ? ' ,
363
- $ websiteId
364
- );
365
- }
366
- $ select ->where ('ce.entity_id IN(?) ' , $ categoryIds );
367
- $ select ->group ('ce.entity_id ' );
368
-
369
- $ countFromCategoryTable = $ this ->_conn ->fetchPairs ($ select );
340
+ $ countFromCategoryTable = $ this ->getCountFromCategoryTable ($ categoryIds , (int )$ websiteId );
370
341
371
342
foreach ($ anchor as $ item ) {
372
343
$ productsCount = 0 ;
@@ -381,6 +352,48 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
381
352
return $ this ;
382
353
}
383
354
355
+ /**
356
+ * Get products number for each category with bulk query
357
+ *
358
+ * @param array $categoryIds
359
+ * @param int $websiteId
360
+ * @return array
361
+ */
362
+ private function getCountFromCategoryTable (
363
+ array $ categoryIds ,
364
+ int $ websiteId
365
+ ) : array {
366
+ $ subSelect = clone $ this ->_conn ->select ();
367
+ $ subSelect ->from (['ce2 ' => $ this ->getTable ('catalog_category_entity ' )], 'ce2.entity_id ' )
368
+ ->where ("ce2.path LIKE CONCAT(ce.path, '/%') " );
369
+
370
+ $ select = clone $ this ->_conn ->select ();
371
+ $ select ->from (
372
+ ['ce ' => $ this ->getTable ('catalog_category_entity ' )],
373
+ 'ce.entity_id '
374
+ );
375
+ $ joinCondition = new \Zend_Db_Expr ("ce.entity_id=cp.category_id OR cp.category_id IN ( {$ subSelect }) " );
376
+ $ select ->joinLeft (
377
+ ['cp ' => $ this ->getProductTable ()],
378
+ $ joinCondition ,
379
+ 'COUNT(DISTINCT cp.product_id) AS product_count '
380
+ );
381
+ if ($ websiteId ) {
382
+ $ select ->join (
383
+ ['w ' => $ this ->getProductWebsiteTable ()],
384
+ 'cp.product_id = w.product_id ' ,
385
+ []
386
+ )->where (
387
+ 'w.website_id = ? ' ,
388
+ $ websiteId
389
+ );
390
+ }
391
+ $ select ->where ('ce.entity_id IN(?) ' , $ categoryIds );
392
+ $ select ->group ('ce.entity_id ' );
393
+
394
+ return $ this ->_conn ->fetchPairs ($ select );
395
+ }
396
+
384
397
/**
385
398
* Add category path filter
386
399
*
0 commit comments