0

Is it possible to programmatically hide products on the front end that has no image?

Running Magento 2.3.

1 Answer 1

-1

You can create two plugin one for "Magento\Catalog\Model\Layer" and another for "Magento\Catalog\Block\Product\ProductList\Toolbar" as describe below to Hide products with no images on frontend

step 1) Create a di.xml under /app/code/Vendor/Module/etc/

File : 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 name="custom_product_model_layer" type="Vendor\Module\Model\Plugin\Layer" />
    </type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="custom_product_toolbar" type="Vendor\Module\Model\Plugin\Toolbar" />
    </type>
</config>

step 2) create Layer.php under /app/code/Vendor/Module/Model/Plugin/

File : Layer.php

<?php

namespace Vendor\Module\Model\Plugin;

use Magento\Catalog\Model\Layer as CatalogLayer;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

class Layer
{

     public function aroundgetProductCollection(\Magento\Catalog\Model\Layer $subject, \Closure $proceed)
    {
        $collection = $proceed();        
        $collection
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
        ->addAttributeToFilter('small_image', array('neq' => ''))
        ->addAttributeToFilter('small_image', array('neq' => null));
        return $collection;
    }   

}

step 3) create Toolbar.php under /app/code/Vendor/Module/Model/Plugin/

File : Toolbar.php

<?php

namespace Vendor\Module\Model\Plugin;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

class Toolbar
{


public function aroundgetProductCollection(\Magento\Catalog\Model\Layer $subject, \Closure $proceed)
    {
        $collection = $proceed();        
        $collection
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
        ->addAttributeToFilter('small_image', array('neq' => ''))
        ->addAttributeToFilter('small_image', array('neq' => null));
        return $collection;
    }    


}  

step 4: Run Below CLI commands

sudo php bin/magento setup:di:compile
11
  • I assume your using a custom Magento 2 Extension name "Vendor_Module" to do the above task. If you are not familiar with how to create custom extension in Magento 2 then you can check this link magestore.com/magento-2-tutorial/magento-2-modules to create a custom extenstion in magento 2. Commented Dec 14, 2018 at 13:13
  • Thank you, i will give it a read shortly and test it out. Commented Dec 14, 2018 at 14:28
  • I'm obviously missing something, i've followed all the steps as above and on the link shared when running setup:di:compile I'm getting an error In PluginList.php line 182: Plugin class Terabyte\NoImageHide\Plugin\Toolbar doesn't exist Commented Dec 16, 2018 at 12:27
  • Can you help me with this please? Commented Dec 18, 2018 at 18:25
  • You may not create the Class Terabyte\NoImageHide\Plugin\Toolbar. Please crate the class as described in my answer and change the namespace to Terabyte\NoImageHide\Model\Plugin; Commented Dec 19, 2018 at 5:54

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.