diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index ded8566a5..daf1f5e9f 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -44,6 +44,9 @@ class MySQL extends AbstractAdapter */ const INNER_JOIN = 'INNER JOIN'; + /** @var bool */ + private $categoryCount = false; + /** * {@inheritdoc} */ @@ -95,6 +98,12 @@ public function getQuery() // Prepare mapping for joined tables $filterToTableMapping = $this->getFieldMapping(); + if ($this->categoryCount === true) { + unset($filterToTableMapping['nleft']); + unset($filterToTableMapping['nright']); + unset($filterToTableMapping['id_group']); + } + // Process and generate all fields for the SQL query below $orderField = $this->computeOrderByField($filterToTableMapping); $selectFields = $this->computeSelectFields($filterToTableMapping); @@ -122,6 +131,18 @@ public function getQuery() } } + + if ($this->categoryCount === true) { + foreach($whereConditions as $key => $oneCondition) { + if (strpos($oneCondition, 'p.id_group') !== false) { + unset($whereConditions[$key]); + } + if (strpos($oneCondition, 'p.nleft') !== false || strpos($oneCondition, 'p.nright') !== false) { + unset($whereConditions[$key]); + } + } + } + // Add where conditions if any if (!empty($whereConditions)) { $query .= ' WHERE ' . implode(' AND ', $whereConditions); @@ -792,8 +813,9 @@ public function count() /** * {@inheritdoc} */ - public function valueCount($fieldName = null) + public function valueCount($fieldName = null, $categoryCount = false) { + $this->categoryCount = $categoryCount; $this->resetGroupBy(); if ($fieldName !== null) { $this->addGroupBy($fieldName); @@ -804,7 +826,6 @@ public function valueCount($fieldName = null) $this->setOrderField(''); $this->copyOperationsFilters(); - return $this->execute(); } diff --git a/src/Filters/Block.php b/src/Filters/Block.php index d9924de81..5be2ae411 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -989,7 +989,25 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) $categories[$value['id_category']] = $value; } - $results = $filteredSearchAdapter->valueCount('id_category'); + $categoryCount = true; + if (true === defined('_PS_FACETED_NEWCOUNT_') && null !== _PS_FACETED_NEWCOUNT_) { + $categoryCount = (bool) _PS_FACETED_NEWCOUNT_; + } + $results = $filteredSearchAdapter->valueCount('id_category', $categoryCount); + + $categoriesId = []; + if ($categoryCount === true) { + $query = new \DbQuery(); + $query->select('id_category'); + $query->from('category'); + $query->where('level_depth <= ' . (int) $parent->level_depth + 1); + $query->where('nleft > ' . $parent->nleft); + $query->where('nright < ' . $parent->nright); + $resultCategories = \Db::getInstance()->executeS($query); + foreach($resultCategories as $oneCategory) { + $categoriesId[] = $oneCategory['id_category']; + } + } foreach ($results as $key => $values) { $idCategory = $values['id_category']; @@ -999,6 +1017,10 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) continue; } + if ($categoryCount && in_array($idCategory, $categoriesId) === false) { + continue; + } + $count = $values['c']; $categoryArray[$idCategory] = [ 'name' => $categories[$idCategory]['name'],