product image grid

If you are trying to add image in product and category grid then you need to do same process
as our previous blog Enhanced Order Grid for Magento 1 opening the commented code in config.xml and add the following files and folder on mentioned path:
Create product.php file in folder ‘app/code/community/Custom/Orders/Block/Adminhtml/Catalog/Category/Tab’ and add below code:

<?php
class Custom_Orders_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('catalog_category_products');
        $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }
 
    public function getCategory()
    {
        return Mage::registry('category');
    }
 
    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in category flag
        if ($column->getId() == 'in_category') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
            }
            elseif(!empty($productIds)) {
                $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
            }
        }
        else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }
 
    protected function _prepareCollection()
    {
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_category'=>1));
        }
        $collection = Mage::getModel('catalog/product')->getCollection()
                          ->addAttributeToSelect('name')
                          ->addAttributeToSelect('sku')
                          ->addAttributeToSelect('price')
                          ->addAttributeToSelect('thumbnail')
                          ->addStoreFilter($this->getRequest()->getParam('store'))
                          ->joinField('position',
                                      'catalog/category_product',
                                      'position',
                                      'product_id=entity_id',
                                      'category_id='.(int) $this->getRequest()->getParam('id', 0),
                                      'left');
        $this->setCollection($collection);
 
        if ($this->getCategory()->getProductsReadonly()) {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
        }
 
        return parent::_prepareCollection();
    }
 
    protected function _prepareColumns()
    {
        if (!$this->getCategory()->getProductsReadonly()) {
            $this->addColumn('in_category', array(
                'header_css_class' => 'a-center',
                'type'      => 'checkbox',
                'name'      => 'in_category',
                'values'    => $this->_getSelectedProducts(),
                'align'     => 'center',
                'index'     => 'entity_id'
            ));
        }
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('catalog')->__('ID'),
            'sortable'  => true,
            'width'     => '60',
            'index'     => 'entity_id'
        ));
        $this->addColumn('name', array(
            'header'    => Mage::helper('catalog')->__('Name'),
            'index'     => 'name'
        ));
        $this->addColumn('image', array(
            'header' => Mage::helper('catalog')->__('Image'),
            'align' => 'left',
            'index' => 'image',
            'width'     => '97',
            'renderer' => 'Custom_Orders_Block_Adminhtml_Template_Grid_Renderer_Image'
        ));
        $this->addColumn('sku', array(
            'header'    => Mage::helper('catalog')->__('SKU'),
            'width'     => '80',
            'index'     => 'sku'
        ));
        $this->addColumn('price', array(
            'header'    => Mage::helper('catalog')->__('Price'),
            'type'  => 'currency',
            'width'     => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index'     => 'price'
        ));
        $this->addColumn('position', array(
            'header'    => Mage::helper('catalog')->__('Position'),
            'width'     => '1',
            'type'      => 'number',
            'index'     => 'position',
            'editable'  => !$this->getCategory()->getProductsReadonly()
            //'renderer'  => 'adminhtml/widget_grid_column_renderer_input'
        ));
 
        return parent::_prepareColumns();
    }
 
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
 
    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('selected_products');
        if (is_null($products)) {
            $products = $this->getCategory()->getProductsPosition();
            return array_keys($products);
        }
        return $products;
    }
 
}
?>

Create grid.php in folder ‘app/code/community/Custom/Orders/Block/Adminhtml/Catalog/Product’ with below code:

<?php
class Custom_Orders_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
	protected function _prepareColumns()
    {
        $this->addColumn('image', array(
            'header' => Mage::helper('catalog')->__('Image'),
            'align' => 'left',
            'index' => 'image',
            'width'     => '70',
            'renderer' => 'Custom_Orders_Block_Adminhtml_Template_Grid_Renderer_Image'
        )); 
        return parent::_prepareColumns();
    }
}
?>

Now add renderer in path ‘app/code/community/Custom/Orders/Block/Adminhtml/Template/Grid/Renderer’:

<?php
class Custom_Orders_Block_Adminhtml_Template_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
	
	//print_r($row->entity_id );
	
	$product = Mage::getModel('catalog/product')->load($row->entity_id);  //load the product     
   //print_r($product->getthumbnail());  
	  //  print_r($product->getThumbnailUrl()); 
	  
	  if($product->getthumbnail() != ''){
	   $out = "<img src=". $product->getThumbnailUrl() ." width='auto' />";
	     }else{
		 
		   $val = Mage::helper('catalog/image')->init($row, 'thumbnail')->resize(97);
		//print_r($val);
        $out = "<img src=". $val ." width='auto' />";
	  
	  }
	  
        return $out;
    }
	
	
}
?>

Related Article

Categories: Blog

0 thoughts on “How to add image on product and category grid”

Leave a Reply

Latest Article

Blog

Samsung Galaxy Mega and Galaxy Grand

Samsung’s unannounced Galaxy O arrangement of cell phones will be VR-Centric and made in a joint effort with Oculus VR, another report now uncovers the cell phones may very well be a piece of two Read more…

Blog

Solt 3-in-1 Windows Tablet

Solt 3-in-1 Windows tablet costs Rs. 19,450. The tablet’s distinguishing strength is its 3-in-1 ease of use. Solt, other than being utilized as a tablet, can likewise be utilized as a portable PC with the Read more…

Blog

Top Tour place In Jaipur – The Pink City

On the off chance that you cherish castles and desire to have an understanding on the way of life of the leaders of the former time, Jaipur is the city for you. On the off Read more…