0

We have 3 levels of categories. When we go to the top-level category page, filter attributes of only parent category are shown. But when we select an attribute filter, attributes of all child categories are also shown. We are using smile elastic suite. On the first load of parent category, it gets the filter items from the following:

protected function _initItems()
{
    parent::_initItems();
    foreach ($this->_items as $item) {
        $applyValue = $item->getLabel();
        if (($valuePos = array_search($applyValue, $this->currentFilterValue)) !== false) {
            $item->setIsSelected(true);
            $applyValue = $this->currentFilterValue;
            unset($applyValue[$valuePos]);
        } else {
            $applyValue = array_merge($this->currentFilterValue, [$applyValue]);
        }
        $item->setApplyFilterValue(array_values($applyValue));
    }
    return $this;
}

This is returning the filter attributes as expected. However, on applying a filter attribute, all attributes of child categories are also shown. The filter is applied as follows:

protected function _getItemsData()
{


    $attribute = $this->getAttributeModel();
    /** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
    $productCollection = $this->getLayer()->getProductCollection();

    $optionsFacetedData = $productCollection->getFacetedData($this->getFilterField());

    $items     = [];

    $attributeCode = $attribute->getAttributeCode();
    if ($attribute->getFrontendInput() == 'multiselect') {
      //echo '1<br/>';
        if($this->_coreRegistry->registry('current_category')) {
        //  echo '2<br/>';
            $catId = $this->_coreRegistry->registry('current_category')->getId();
            $facetSession = $this->_catalogSession->getData('multiselect_facets');
            if(isset($facetSession[$catId][$attributeCode]) && isset($_GET[$attributeCode])) {;
                $optionsFacetedData = $facetSession[$catId][$attributeCode];
            } else {
            //  echo '4<br/>';
                $optionsFacetedData = $productCollection->getFacetedData($this->getFilterField());
        // $optionsFacetedData returning all attributes after filter
                $facetArray = $facetSession;
                $facetArray[$catId][$attributeCode] = $optionsFacetedData;
                $this->_catalogSession->setData('multiselect_facets', $facetArray);
            }
        } else {
            $catId = 'search';
            $facetSession = $this->_catalogSession->getData('multiselect_facets');

            if(isset($facetSession[$catId][$attributeCode]) && isset($_GET[$attributeCode])) {

                $optionsFacetedData = $facetSession[$catId][$attributeCode];
            } else {
                $optionsFacetedData = $productCollection->getFacetedData($this->getFilterField());
                $facetArray = $facetSession;
                $facetArray[$catId][$attributeCode] = $optionsFacetedData;

                $this->_catalogSession->setData('multiselect_facets', $facetArray);
            }
        }
    }

    if (isset($optionsFacetedData['__other_docs'])) {
        $this->hasMoreItems = $optionsFacetedData['__other_docs']['count'] > 0;
        unset($optionsFacetedData['__other_docs']);
    }

    foreach ($optionsFacetedData as $value => $data) {
        $items[$value] = [
            'label' => $this->tagFilter->filter($value),
            'value' => $value,
            'count' => $data['count'],
        ];
    }

    $items = $this->addOptionsData($items);

    return $items;
}

Here, $productCollection->getFacetedData($this->getFilterField()) is returning data of all child category attributes. How to change this behaviour so that after applying a filter only top level category attributes are shown in filter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.