0

We need to hide certain products on both pages, Category/Search result page. We have an attribute called "discontinued" of Yes/No type. If it is set to yes, then products should not be in the category page and search result page.

Only when search with sku, it should go to the product page. For now, when we search with sku, it goes to the product page, because we enable Goto product page if only one search result. We just want to remove them from category and search result page.

Please reply.

1 Answer 1

1

For this you can try below code.

Create plugin in di.xml file

app/code/[vendor]/[Module]/etc/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">
        <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Catalog_Model_Layer" sortOrder="10" type="[vendor]\[Module]\Plugin\Magento\Catalog\Model\Layer"/>
    </type>
</config>

Now create Plugin PHP file

[vendor]\[Module]\Plugin\Magento\Catalog\Model\Layer.php

<?php

namespace [vendor]\[Module]\Plugin\Magento\Catalog\Model;

class Layer
{

    public function aroundGetProductCollection(
        \Magento\Catalog\Model\Layer $subject,
        \Closure $proceed
    ) {
        $collection = $proceed();
        $collection
            ->addAttributeToSelect('*')
            ->addAttributeToFilter(array(
                array(
                    'attribute' => 'discontinue',
                    'null' => true),
                array(
                    'attribute' => 'discontinue',
                    'eq' => '0')
            ));
        return $collection;
    }


}

Note: Before this plugin please create discontinue product attribute and place your attribute code in plugin file

Your Answer

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

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