display if product has images in product grid andmin area magent 1.9

by lazy_coder   Last Updated May 15, 2019 15:09 PM - source

In magento back office, I am adding a field called 'has image' in product Grid with values yes/no

I successfully added the new column in the grid but when I try to apply the filer 'no' I am not getting the product with image is null and thumbnail and small_image is null

this is my code:

    protected function _prepareCollection()
{
    parent::_prepareCollection();
    /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
    $collection = $this->getCollection();
    $collection->addAttributeToSelect('small_image, thumbnail, image');
    $this->setCollection($collection);

    return $this;
}

protected function _prepareColumns()
{
    $this->addColumnAfter('has_image',
        array(
            'header'=> $this->__('Has images'),
            'width' => '70px',
            'index' => 'image',
            'type'      => 'options',
            'options'   => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
            'renderer' => 'custom_Catalog_Block_Adminhtml_Product_Renderer_HasImage',
            'filter_condition_callback' => array($this, 'hasImageFilter'),
            ),
        'name');
    return parent::_prepareColumns();
}




protected function hasImageFilter($collection, $column) {
    if ($column->getFilter()->getValue() == '1') {
        $collection->addAttributeToFilter(
        array(
            array('attribute' => 'image', ['neq' => 'no_selection', 'null' => false]),
            array('attribute' => 'small_image', ['neq' => 'no_selection', 'null' => false]),
            array('attribute' => 'thumbnail', ['neq' => 'no_selection', 'null' => false]),
        )
    );
        Mage::log('***********************************');
        Mage::log($collection->getSelectSql(true));
        return $this;
    }

    $collection->addAttributeToFilter( array(
        array('attribute' => 'image', 'eq' => 'no_selection'),
        array('attribute' => 'image', 'null' => true),
        array('attribute' => 'image', 'eq' => 'NULL'),
        array('attribute' => 'image', 'nlike' => '%/%/%'),
        )
    );

    $collection->addAttributeToFilter( array(
        array('attribute' => 'small_image', 'eq' => 'no_selection'),
        array('attribute' => 'small_image', 'null' => true),
        )
    );
    $collection->addAttributeToFilter( array(
        array('attribute' => 'thumbnail', 'eq' => 'no_selection'),
        array('attribute' => 'thumbnail', 'null' => true),
        array('attribute' => 'thumbnail', 'nlike' => '%/%/%'),
        )
    );


    Mage::log('***********************************');
    Mage::log($collection->getSelectSql(true));

    return $this;
}

Any help would be appreciate.



Related Questions



Grid Filter From: To:

Updated September 05, 2017 16:09 PM

Magento-2 catalog product grid refresh issue

Updated May 05, 2018 20:09 PM

Product grid not posting values M2

Updated July 10, 2018 10:09 AM

inline edit in product grid

Updated July 16, 2018 09:09 AM