2

I have a query and want to know about this How Magento 2 filtration is working from layered navigation. I have a scenario, There is a configurable product in Magento 2.2 and having 6 associate products with color red, blue, green and size 28, 29

My requirement is that there is the option in admin where admin can disabled color by choose colors(Multiselect). In frontend product list page, that color should not be shown. I have made configuration for disabling color in admin.

To achieve this, I have override getUsedProductCollection() in Magento\ConfigurableProduct\Model\Product\Type\Configurable

public function getUsedProductCollection($product){
    $filterColor = array('121'); // where 123 is red color value id. 
    $collection = $this->_productCollectionFactory->create()->setFlag(
        'require_stock_items',
        true
    )->setFlag(
        'product_children',
        true
    )->setProductFilter(
        $product
    );
    if (!is_null($this->getStoreFilter($product))) {
        $collection->addStoreFilter($this->getStoreFilter($product));
    }

    if(!empty($filterColor)){
        $collection->addAttributeToFilter(array(
            array('attribute' => 'color','nin'=>$filterColor)
        ));
    }
    return $collection;
}

It works, once category product load but has the issue with layered navigation filter. According to my requirement that if user selects red color from layered navigation. Configurable products shouldn't show red color products because we have disable from collection.

  • Did you check if the same collection is being used after applying the layered navigation filter? – Shireen N Feb 5 '18 at 8:47

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.