Magento 2 How to hide products from entire shop based on custom attribute value

by Jessy Loran   Last Updated September 18, 2018 11:09 AM

Is there any way by custom code or plugin, products can be hide from entire frontend shop based on custom attribute type yes or no ?



Answers 1


The Magento standard way would be to use the visibility attribute but if you have to do it with a custom attribute here's something that'll get you started, and may be all you need.

There isn't one function that can overridden to completely control product visibility but this is a good way to control the visibility of products on category pages.

etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer\Category\CollectionFilter">
        <plugin name="vendorname_modulename_catalog_model_layer_category_collectionfilter" type="VendorName\ModuleName\Plugin\CollectionFilter"/>
    </type>
</config>

Plugin/CollectionFilter.php

<?php

namespace VendorName\ModuleName\Plugin;

class CollectionFilter
{
    public function afterFilter(
        \Magento\Catalog\Model\Layer\Category\CollectionFilter $subject,
        $result,
        \Magento\CatalogSearch\Model\ResourceModel\FullText\Collection $collection,
        \Magento\Catalog\Model\Category $category
    ) {
        $collection->addAttributeToFilter('custom_attribute', 'custom_value');
    }
}
Media Lounge
Media Lounge
September 18, 2018 11:39 AM

Related Questions


Magento 2 How to add custom product relations?

Updated January 03, 2018 20:09 PM


How to display color and size in magento 2

Updated October 11, 2018 20:09 PM


Magento 2 Plugin seemingly not working

Updated July 04, 2018 11:09 AM