Amasty Resolution Center

Issue with Amasty Improved Layered Navigation - Category-Specific Filters Inconsistency

Environment Details

  • Magento Version: 2.4.6

Issue Description

I'm experiencing an issue with the Amasty Improved Layered Navigation module where I'm fetching layered navigation category filters programmatically in a controller and in a template file, but receiving all possible filters with every possible item in controller, instead of only category-specific ones which I am getting in the template file.

For example, the example snippet of the data returned in the template can be seen in template data.json and example snippet of data returned in the controller can be seen in controller data.json

Technical Details

I'm using a custom controller to fetch filters for a specific category. Despite properly setting the category context and applying category filters to the product collection, the returned filters include options that should not be available in the current category context. This is how I am handling filter fetching in the controller:

  1. public function execute()
    {
    $result = $this->jsonResultFactory->create();
    $filterArray = [];

    try {
    $categoryId = $this->getRequest()->getParam('category');
    if (!$categoryId) {
    throw new Exception('Category ID is required');
    }

    // Get category
    $category = $this->categoryRepository->get($categoryId);

    // Get layer and set current category
    $layer = $this->layerResolver->get();
    $layer->setCurrentCategory($category);

    // Get product collection for the category
    $productCollection = $layer->getProductCollection();
    $productCollection->addCategoryFilter($category);

    // Apply the product collection to the layer
    $layer->prepareProductCollection($productCollection);

    // Get filters based on the prepared collection
    $filters = $this->filterList->getFilters($layer);

    // Process displayed filters
    $unsortedDisplayedFilterList = $this->filterHelper->getDisplayedFilterList($filters);
    $displayedFilterList = $this->filterHelper->sortFilters($unsortedDisplayedFilterList);

    foreach ($displayedFilterList as $filter) {
    if (!$filter->getItemsCount()) {
    continue; // Skip empty filters
    }

    $items = $filter->getItems();
    if (empty($items)) {
    continue; // Skip filters with no items
    }

    $filterData = [
    'name' => $filter->getName(),
    'request_var' => $filter->getRequestVar(),
    'items' => []
    ];

    foreach ($items as $item) {
    if ($item->getCount() > 0) { // Only include items with products
    $filterData['items'][] = [
    'label' => $item->getLabel(),
    'value' => $item->getValue(),
    'count' => $item->getCount(),
    ];
    }
    }

    if (!empty($filterData['items'])) {
    $filterArray[] = $filterData;
    }
    }

    $result->setData($filterArray);

    } catch (Exception $e) {
    $result->setData(['error' => $e->getMessage()]);
    }

    return $result;
    }

Question

Is there a specific approach or additional configuration required when programmatically fetching category-specific filters using Amasty's Improved Layered Navigation? Do I need to implement additional logic to restrict filters to only show options available in the current category? I want to get the same data as in the template file. 

Thank you for your assistance