1

I am using following code to show product thumbnails in category products tab in admin panel. Magento Version 1.9.1.1

app/etc/modules/My_Thumbnail.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Thumbnail>
            <active>true</active>
            <codePool>local</codePool>
        </My_Thumbnail>
    </modules>
</config>

app/code/local/My/Thumbnail/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Thumbnail>
            <version>1.0.0.2</version>
        </My_Thumbnail>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>My_Thumbnail_Block_Adminhtml_Catalog_Category_Tab_Product</catalog_category_tab_product>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

Then Copied app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php to app/code/local/My/Thumbnail/Block/Adminhtml/Catalog/Category/Tab/Product.php and edited as follows:

<?php
class My_Thumbnail_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' => 'My_Thumbnail_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;
}

}

Then Created a renderer at app/code/local/My/Thumbnail/Block/Adminhtml/Template/Grid/Renderer/Image.php

 <?php
class My_Thumbnail_Block_Adminhtml_Template_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
    $val = Mage::helper('catalog/image')->init($row, 'thumbnail')->resize(97);
    $out = "<img src=". $val ." width='97px'/>";
    return $out;
}
}

But No Columns have been added to the product grid and no images are shown. Any Help?

2

In this case, you need to check each file residing in the folder app/etc/modules whether any file is replacing the category_product_tab and if there is some then merge that module with your one and you can enjoy both of your plugins.

  • 1
    Thanks @Vashikaran Samrat. This is a good advise. I will check through this for my new plugin. – Pt. Raman Sharma Jan 10 '16 at 11:00
1

I have figured it out. This code is correct and has no problems. In my magento, I had previously installed a plugin for edit links in category products tab which I uninstalled and it worked.

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.