Skip to content

Commit

Permalink
Merge pull request #124 from tatiana-scandi/bugfix/3345/fix-layered-n…
Browse files Browse the repository at this point in the history
…avigation-sr

#3345 - fix layered navigation on search results page
  • Loading branch information
carinadues authored Sep 24, 2021
2 parents 1728b6e + ebd3fc3 commit dd6cb78
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions src/Model/Resolver/Aggregations.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;

class Aggregations extends AggregationsBase {
Expand All @@ -27,6 +28,11 @@ class Aggregations extends AggregationsBase {
*/
protected $attribute;

/**
* @var CategoryRepository
*/
protected $categoryRepository;

/**
* Code of the price attribute in aggregations
*/
Expand All @@ -37,13 +43,19 @@ class Aggregations extends AggregationsBase {
*/
public const CATEGORY_ID_CODE = 'category_id';

/**
* ID of the top level menu items
*/
public const TOP_NAVIGATION_LEVEL_ID = 2;

/**
* @inheritdoc
*/
public function __construct(
Filters $filtersDataProvider,
LayerBuilder $layerBuilder,
Attribute $attribute
Attribute $attribute,
CategoryRepository $categoryRepository
)
{
parent::__construct(
Expand All @@ -52,6 +64,7 @@ public function __construct(
);

$this->attribute = $attribute;
$this->categoryRepository = $categoryRepository;
}

/**
Expand All @@ -66,8 +79,15 @@ public function resolve(
) {
$result = parent::resolve($field, $context, $info, $value);

$isSearch = isset($value['layer_type']) && $value['layer_type'] == 'search';

$result = $this->processPriceFilter($result);
$result = $this->enhanceAttributes($result);
$result = $this->enhanceAttributes($result, $isSearch);

// on the search results page we should show only top level categories
if($isSearch){
$result = $this->removeNonTopLevelCategories($result);
}

return $result;
}
Expand Down Expand Up @@ -106,7 +126,7 @@ protected function processPriceFilter(array $result): array {
* @param array $result Filters
* @return array
*/
protected function enhanceAttributes(array $result): array {
protected function enhanceAttributes(array $result, $isSearch): array {
foreach ($result as $attr => $attrGroup) {
// Category ID is not a real attribute in Magento, so needs special handling
if($attrGroup['attribute_code'] == self::CATEGORY_ID_CODE){
Expand All @@ -118,6 +138,14 @@ protected function enhanceAttributes(array $result): array {

$attribute = $this->attribute->loadByCode('catalog_product', $attrGroup['attribute_code']);

// Hide attributes based on the settings
if($isSearch){
if(!$attribute->getIsFilterableInSearch()){
unset($result[$attr]);
continue;
}
}

// Add flag to indicate that attribute is boolean (Yes/No, Enable/Disable, etc.)
$result[$attr]['is_boolean'] = $attribute->getFrontendInput() === 'boolean';

Expand All @@ -136,4 +164,28 @@ protected function enhanceAttributes(array $result): array {

return $result;
}

protected function removeNonTopLevelCategories(array $result) : array {
foreach ($result as $attr => $attrGroup){
if($attrGroup['attribute_code'] == self::CATEGORY_ID_CODE){
$newOptions = [];

foreach ($attrGroup['options'] as $option) {
$category = $this->categoryRepository->get($option['value']);

if(!$category->getIsActive()){
continue;
}

if($category->getLevel() == self::TOP_NAVIGATION_LEVEL_ID){
$newOptions[] = $option;
}
}

$result[$attr]['options'] = $newOptions;
}
}

return $result;
}
}

0 comments on commit dd6cb78

Please sign in to comment.