Skip to content

Discrepancy in Filter Items between Controller and Template

I am kinda stuck on this one issue. I am retrieving the category filters in the controller. The same filters are also retrieved in the template file. The issue is that in the controller, the same function retrieves all available filter options, but the template only shows a subset of these options.
Example:
Color Filter in template: Only one option is shown (Blue).
Color Filter in controller: All color options are available (Black, Blue, Gray, etc.).
This difference seems to occur across all filters (not just the color filter), though the severity of the difference can vary based on the filter.

FetchFilters.php (Controller)

<?php
/**
 * @category  Gearup
 * @package   Gearup_KlevuSearch
 * @author    Scandiweb <[email protected]>
 * @copyright Copyright (c) 2024 Scandiweb, Inc (https://scandiweb.com)
 * @license   http://opensource.org/licenses/OSL-3.0 The Open Software License 3.0 (OSL-3.0)
 */

namespace GearupFiltersControllerIndex;

use Exception;
use MagentoCatalogModelCategoryRepository;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultJsonFactory;
use MagentoCatalogModelLayerResolver;
use MagentoCatalogModelLayerFilterList;
use MagentoCatalogModelLayerAvailabilityFlagInterface;
use GearupAmastyShopbyHelperFilter as FilterHelper;
use MagentoCatalogModelLayerCategoryFilterableAttributeList;
use MagentoCatalogModelProductAttributeSourceStatus;
use MagentoCatalogModelProductVisibility;
use MagentoCatalogModelLayerFilterListFactory;

class FetchFilters extends Action
{

    protected $jsonResultFactory;

    protected $layerResolver;

    protected $filterList;

    protected $visibilityFlag;

    protected $filterableAttributeList;

    protected $filterListFactory;

    public function __construct(
        Context $context,
        JsonFactory $jsonResultFactory,
        Resolver $layerResolver,
        FilterList $filterList,
        AvailabilityFlagInterface $visibilityFlag,
        FilterHelper $filterHelper,
        FilterableAttributeList $filterableAttributeList,
        CategoryRepository $categoryRepository,
        FilterListFactory $filterListFactory
    ) {
        parent::__construct($context);
        $this->jsonResultFactory = $jsonResultFactory;
        $this->layerResolver = $layerResolver;
        $this->filterList = $filterList;
        $this->visibilityFlag = $visibilityFlag;
        $this->filterHelper = $filterHelper;
        $this->filterableAttributeList = $filterableAttributeList;
        $this->categoryRepository = $categoryRepository;
        $this->filterListFactory = $filterListFactory;
    }

    public function execute()
    {
        $result = $this->jsonResultFactory->create();

        try {
            // In your controller, before getting filters:
            $categoryId = 40;
            $category = $this->categoryRepository->get($categoryId);
            $layer = $this->layerResolver->get();
            $layer->setCurrentCategory($category);

            // Add these lines to apply filters first
            foreach ($this->filterList->getFilters($layer) as $filter) {
                $filter->apply($this->getRequest());
            }
            $layer->apply();

            // Now get your filters - they will match what the template sees
            $filters = $this->filterList->getFilters($layer);
            $unsortedDisplayedFilterList = $this->filterHelper->getDisplayedFilterList($filters);
            $displayedFilterList = $this->filterHelper->sortFilters($unsortedDisplayedFilterList);

            foreach ($displayedFilterList as $filter) {
                $newArray = [];
                $items = $filter->getItems();
                $newArray['name'] = $filter->getName();
                $newArray['request_var'] = $filter->getRequestVar();
                foreach ($items as $item) {
                    $newArray['items'][] = [
                        'label' => $item->getLabel(),
                        'value' => $item->getValue(),
                        'count' => $item->getCount(),
                    ];
                }
                $filterArray[] = $newArray;
            }

            $result->setData($filterArray);
        } catch (Exception $e) {
            $result = $this->jsonResultFactory->create();
            $result->setData(['error' => $e->getMessage()]);
        }
        return $result;
    }
}

Template File

<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Shop By Hyva Compatibility
 */

/**
 * @category  Gearup
 * @package   Gearup_Hyva
 * @author    Scandiweb <[email protected]>
 * @copyright Copyright (c) 2024 Scandiweb, Inc (https://scandiweb.com)
 * @license   http://opensource.org/licenses/OSL-3.0 The Open Software License 3.0 (OSL-3.0)
 */

/**
 * Overridden to:
 * - Remove state block
 * - Configure element style
 */

declare(strict_types=1);

use AmastyShopbyApiDataFromToFilterInterface;
use AmastyShopbyHyvaCompatibilityViewModelJsInit;
use AmastyShopbyHyvaCompatibilityViewModelProductListAjax;
use HyvaThemeModelViewModelRegistry;
use HyvaThemeViewModelHeroiconsOutline;
use MagentoFrameworkEscaper;
use MagentoLayeredNavigationBlockNavigation;
use HyvaThemeViewModelSvgIcons;
use GearupAmastyShopbyHelperFilter as FilterHelper;
use MagentoCatalogApiDataProductInterface;
use HyvaThemeViewModelHeroiconsSolid;

// phpcs:disable Generic.Files.LineLength.TooLong

/** @var Navigation $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var JsInit $jsInitViewModel */
$jsInitViewModel = $viewModels->require(JsInit::class);

/** @var HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);

/** @var ProductListAjax $productListAjax */
$productListAjax = $viewModels->require(ProductListAjax::class);

$isSidebar = $block->getData('isSidebar');
$isDropDrown = false;

/** @var HeroiconsSolid $heroIcons */
$heroIconsSolid = $viewModels->require(HeroiconsSolid::class);

/** @var SvgIcons $hyvaicons */
$hyvaicons = $viewModels->require(SvgIcons::class);

/** @var FilterHelper $filterHelper */
$filterHelper = $this->helper(FilterHelper::class);

$filters = $block->getFilters();