1

I am not so much familiar with magento development.i am facing some trouble my problem is i have add product's associated categories within product name column in admin panel(catalog > manage products) & i have also filter product by categories and i don't have any idea.how to do this task.

Thanks in advance for help

2
  • have you done any thing yet. share the code you tried so far? – Qaisar Satti Jun 3 '15 at 12:51
  • @QaisarSatti no i don't have any idea & i am just searching & studying for this – Dinesh Jun 3 '15 at 13:02
4

You can rewrite class Mage_Adminhtml_Block_Catalog_Product_Grid in your custom module or put this file into local/Mage/Adminhtml/Block/Catalog/Product.

1) In _prepareColumns method add the following code:

$this->addColumn(
    'category_id',
    array(
        'header'                    => $this->__('Category'),
        'width'                     => '100px',
        'index'                     => 'category_ids',
        'type'                      => 'options',
        'options'                   => $this->getCategoryOptions(),
        'filter_condition_callback' => array($this, '_callbackCategoryFilter'),
        'renderer'                  => '***custom_module***/adminhtml_catalog_product_render_category'
    )
);

2) Create here in class your custom method

public function getCategoryOptions()
{
    $option_array = array();
    $category_collection = Mage::getResourceModel('catalog/category_collection')
        ->addNameToResult()
        ->addAttributeToSort('position', 'asc');
    foreach ($category_collection as $category) {
        if ($category->getId() > 1 && $category->getName() != 'Root Catalog') {
            $option_array[$category->getId()] = $category->getName();
        }
    }
    return $option_array;
}

Or, which is preferable, create this method in your custom helper.

3) Create here in class protected method _callbackCategoryFilter

protected function _callbackCategoryFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return null;
    }
    $collection->joinField(
        'category_id',
        'catalog/category_product',
        'category_id',
        'product_id = entity_id',
        '{{table}}.category_id=' . $column->getFilter()->getValue(),
        'inner'
    );
}

4) Create renderer:

class ***Company_Module***_Block_Adminhtml_Catalog_Product_Render_Category
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $productCategories = array();
        $product = Mage::getModel('catalog/product')->load($row->getData('entity_id'));
        $categories = $product->getCategoryCollection()->addAttributeToSelect('name');
        foreach ($categories as $category) {
            array_push($productCategories, $category->getName());
        }
        return implode('<br>', $productCategories);
    }
}

The result: The result

1
  • This Works Fine but there is a problem of page load time, this render function increases loading time of your catalog grid page. Bcoz in this render function, array creates for every products every time. – Vishal Thakur Jan 13 '17 at 12:10
1

The above code Works Fine but there is a problem of page load time, this render function increases loading time of your catalog grid page. Bcoz in this render function, array creates for every products every time.

To Overcome from this problem you need to do some changes in your render function() as below:-

class ***Company_Module***_Block_Adminhtml_Catalog_Product_Render_Category
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
  {
    $product = Mage::getModel('catalog/product')->load($row->getEntityId());
    $cats = $product->getCategoryIds();
    $allCats = '';
    foreach($cats as $key => $cat)
    {
        $_category = Mage::getModel('catalog/category')->load($cat);
        $allCats.= $_category->getName();
        if($key < count($cats)-1)
            $allCats.= ',<br />';
    }
    return $allCats;
  }
}

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.