0

I can't find how to do this in Magento 2 (plenty of info on magento 1, but I don't know if it is the same code?).

In the product collection code, when adding addAttributeToFilter() - what code do I need to use to filter out products that are missing an image?

Something like this (from Magento 1 code) maybe?

->addAttributeToFilter('image', array("neq"=>'no_selection'))

Could someone confirm please?

2
  • This should work don't see why it wouldn't Sep 14, 2017 at 18:32
  • I guess I should have just tried it before posting!! Works fine :-)
    – robgt
    Sep 18, 2017 at 10:15

1 Answer 1

3

In Magento 2, yourModule/etc/di.xml

<type name="Magento\Catalog\Model\Layer">

<plugin name="exclude_no_image_product" type="yourCompany\yourModule\Model\Rewrite\LayerPlugin"/>

  </type>

Add file yourCompany\yourModule\Model\Rewrite\LayerPlugin.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace yourCompany\yourModule\Model\Rewrite;

/**
 * Catalog view layer model
 *
 * @api
 * @SuppressWarnings(PHPMD.LongVariable)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @since 100.0.2
 */
class LayerPlugin
{ 
    /**
     * Retrieve current layer product collection
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    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;
    }    
}

It will work. I have found and i am using in my website. Learn more.. www.webizon.in

Your Answer

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

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