0

I'm trying to add a column on the catalog_product grid on magento 1.9.0.1. I want my column to contain the number of images of each product.

I have created an extension for that.

In config.xml

<catalog_product_collection_load_before>
    <observers>
        <event_sales_order_grid_collection_load_before>
            <type>singleton</type>             
            <class>Group_Module_Model_Observer</class>
            <method>catalogProductGridCollectionLoadBefore</method>
        </event_sales_order_grid_collection_load_before>
    </observers>
</catalog_product_collection_load_before>
<core_block_abstract_prepare_layout_after>
    <observers>
    <add_column_product_by_categ_nbr_image>
        <type>singleton</type>
            <class>Group_Module_Model_Observer</class>
        <method>addNbImagesColonne</method>
        </add_column_product_by_categ_nbr_image>
    </observers>
</core_block_abstract_prepare_layout_after>

In observer.php :

public function addNbImagesColonne($observer){
            $block = $observer->getEvent()->getBlock();

            if(get_class($block) == 'Mage_Adminhtml_Block_Catalog_Product_Grid'){
                $block->addColumnAfter('nbrImages', array(
                        'header'    => "Number of images",
                        'width'     => '100px',
                        'index'     => 'nbImage',
                        'type'        => 'text',
                        'filter_index' => 'cpemg.nbImage',
                ), 'status');
            }
        }
public function catalogProductGridCollectionLoadBefore($observer)
    {

        $collection = $observer->getCollection();//get the collection

        $collection->calculateSizeWithoutGroupClause = true;//Correct the number of row return
        $select = $collection->getSelect();

        $select->joinLeft(
                array('cpemg' => Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_media_gallery')),
                'cpemg.entity_id=e.entity_id',
                array(
                        'nbImage' => new Zend_Db_Expr(' COUNT(cpemg.value_id)'),
                )
        );
        $select->group('e.entity_id');

    }

My column is working fine but when I try to filter products by the number of image, I have no error print and my grid does not show.

Could you tell me where my mistake is ?

0

filter_index is afaik the column you want to filter on. Did you add a column nbImage to your table catalog_product_entity_media_gallery? If not, try to remove the cpemg right before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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