0

I created a custom filter to filter products on category pages and in search. Filter itself seems to be correct (two products are visible from origin twelve) but pagination is wrong (total size is still 12 and link to 2nd page is rendered) (see 1st screenshot: ).

This filter is also not taken into account when list of other filters is rendered (see 2nd screeshot: there is still 12 products calculate instead of 2).

Filter itself is rather simple:

public function apply(\Magento\Framework\App\RequestInterface $request)
{
    $filter = $request->getParam($this->getRequestVar());
    if (!$filter) {
        return $this;
    }

    $collection = $this->getLayer()->getProductCollection();
    $customCollection = $this->collectionFactory->create();
    $customCollection->addFieldToFilter('value', $filter);
    $customIds = $customCollection->getColumnValues('entity_id');

    $collection->addAttributeToFilter([['attribute' => 'available_custom_ids',['in' => $customIds]],['attribute' =>'available_custom_ids',['null'=>true]]], null, 'left');
    $this->getLayer()->getState()->addFilter($this->_createItem($filter, $filter));
    return $this;
}

I injected this filter to the \Magento\Catalog\Model\Layer\FilterList with:

public function aroundGetFilters(
    \Magento\Catalog\Model\Layer\FilterList $subject,
    \Closure $proceed,
    \Magento\Catalog\Model\Layer $layer
) {
    $result = $proceed($layer);
    $result[] = $this->_objectManager->create(
        \Vendor\Module\Model\Layer\Filter\ExampleFilter::class,
        ['layer' => $layer]
    );

    return $result;
  }

What I'm doing wrong? It seems Magento is using two different collections on the same page, but I'm totally lost which is which (and why).

Side note: I used custom filter instead of simple make "available_custom_ids" filterable because I wanted to include also products with available_custom_ids == NULL to the results. Is it correct approach?

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.