3

I have used Magento Plugin concept for custom collection on the category page. Everything is working fine except price filter in layered navigation.

Below code of di.xml

<type name="Magento\Catalog\Block\Product\ListProduct">
    <plugin name="mynamespace-mymodule-block-product-list"
            type="Mynamespace\Mymodule\Plugin\ProductList"
            sortOrder="10" />
</type>

Below code of ProductList.php

<?php 
public function beforeToHtml(\Magento\Catalog\Block\Product\ListProduct $subject )
{       
    $skus = array('sku1','sku2');

    $layer = $subject->getLayer();

    $pcollection = $layer->getProductCollection()->clear()->addAttributeToFilter('sku', array('in' => $skus));      
    $pcollection->applyFrontendPriceLimitations();
    $pcollection->getSize();
    $subject->setCollection($pcollection);

    $layer->setProductCollection($pcollection);

}
?>

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.