@@ -337,63 +337,16 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
337337 $ categoryIds = array_keys ($ anchor );
338338 $ countSelect = $ this ->getProductsCountQuery ($ categoryIds , (bool )$ websiteId );
339339 $ categoryProductsCount = $ this ->_conn ->fetchPairs ($ countSelect );
340- $ countFromCategoryTable = $ this ->getCountFromCategoryTable ($ categoryIds , (int )$ websiteId );
341-
342340 foreach ($ anchor as $ item ) {
343- $ productsCount = 0 ;
344- if (isset ($ categoryProductsCount [$ item ->getId ()])) {
345- $ productsCount = (int )$ categoryProductsCount [$ item ->getId ()];
346- } elseif (isset ($ countFromCategoryTable [$ item ->getId ()])) {
347- $ productsCount = (int )$ countFromCategoryTable [$ item ->getId ()];
348- }
341+ $ productsCount = isset ($ categoryProductsCount [$ item ->getId ()])
342+ ? (int )$ categoryProductsCount [$ item ->getId ()]
343+ : $ this ->getProductsCountFromCategoryTable ($ item , $ websiteId );
349344 $ item ->setProductCount ($ productsCount );
350345 }
351346 }
352347 return $ this ;
353348 }
354349
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-
397350 /**
398351 * Add category path filter
399352 *
@@ -566,6 +519,45 @@ public function getProductTable()
566519 return $ this ->_productTable ;
567520 }
568521
522+ /**
523+ * Get products count using catalog_category_entity table
524+ *
525+ * @param Category $item
526+ * @param string $websiteId
527+ * @return int
528+ */
529+ private function getProductsCountFromCategoryTable (Category $ item , string $ websiteId ): int
530+ {
531+ $ productCount = 0 ;
532+
533+ if ($ item ->getAllChildren ()) {
534+ $ bind = ['entity_id ' => $ item ->getId (), 'c_path ' => $ item ->getPath () . '/% ' ];
535+ $ select = $ this ->_conn ->select ();
536+ $ select ->from (
537+ ['main_table ' => $ this ->getProductTable ()],
538+ new \Zend_Db_Expr ('COUNT(DISTINCT main_table.product_id) ' )
539+ )->joinInner (
540+ ['e ' => $ this ->getTable ('catalog_category_entity ' )],
541+ 'main_table.category_id=e.entity_id ' ,
542+ []
543+ )->where (
544+ '(e.entity_id = :entity_id OR e.path LIKE :c_path) '
545+ );
546+ if ($ websiteId ) {
547+ $ select ->join (
548+ ['w ' => $ this ->getProductWebsiteTable ()],
549+ 'main_table.product_id = w.product_id ' ,
550+ []
551+ )->where (
552+ 'w.website_id = ? ' ,
553+ $ websiteId
554+ );
555+ }
556+ $ productCount = (int )$ this ->_conn ->fetchOne ($ select , $ bind );
557+ }
558+ return $ productCount ;
559+ }
560+
569561 /**
570562 * Get query for retrieve count of products per category
571563 *
0 commit comments