4

I am filtering the product collection in ListProduct.php using below code

// Sample sku array
$skuarray=array('sku1', 'sku2', 'sku3');

$this->_productCollection = $layer->getProductCollection()->addAttributeToFilter('sku', array('in' => $skuarray));

Now i am getting a list of products filtered by SKU in my view but, Layer navigation is still displaying the all filters(attributes) which is in category.

For example:
Category having 20 products, 
i am filtering only 3 products by using sku filters,
Products are displaying filtered 3 products
But Left side Layer Navigation Displaying filters for 20 products i.e category is having

2 Answers 2

1

You can try override initializeProductCollection function,

add your filter before catalog_block_product_list_collection event.

effect from catalog_block_product_list_collection

1
  • Thanks for your information. Could you please elaborate this one.
    – Ramesh KR
    Dec 4, 2019 at 6:15
0

I didn't examine layered nav in M2 yet but if it works like in M1 your issue could depend on the fact that filters and result use two different layer instances, that access to different collection objects.

That means that your addAttributeToFilter() is applied to result collection but not to filters collection and it could explain the unexpected result you get.

2
  • 1
    Hi Alessandro, im facing similar issue, like my custom product collection loads, but when filters are applied to it, they are not getting affected.. I have overridden Magento\Catalog\Block\Product\ListProduct.php's _getProductCollection() and \Magento\LayeredNavigation\Block\Navigation.php's canShowBlock() only, as there is no prodCollection() here
    – Sushivam
    Sep 28, 2016 at 11:51
  • Hello Everyone Just to Add to you. If you Override ListProduct.php Block then it will not affect the Layer Navigation Collection as Layer Navigation Collection can be affected only and only by Overriding Layer Model Product Collection. can't say about Magento 2 as i have not tried it. but in Magento 1, it works by Overriding Model Class for Collection.
    – Shashank
    Oct 21, 2016 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.