Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

add categories column to the product grid in magento admin
 
pablop
Jr. Member
 
Total Posts:  14
Joined:  2010-01-27
 

Hi,

I’m trying to add a column with a list of categories to the admin product grid.
Following another post I modified Mage_Adminhtml_Block_Catalog_Product_Grid
http://www.magentocommerce.com/boards/viewthread/44534/

protected function _prepareCollection()
 
{
    $store 
$this->_getStore();
    
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            ->
joinTable('catalog/category_product_index',
                
'product_id=entity_id',
                array(
'category_id'),
                
null,
                
'left')
            ->
groupByAttribute('entity_id')
            ->
addExpressionAttributeToSelect('category_id',
                
'GROUP_CONCAT(category_id SEPARATOR " ; ")',
                
'category_ids')
            
//->addAttributeToSelect('category_id')
            //->addStaticField('category_id')
            
->joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left');
           ...

protected function 
_prepareColumns()
    
{
        $this
->addColumn('category_id',
            array(
                
'header'=> Mage::helper('catalog')->__('Categories'),
                
'index' => 'category_id',
                
'width' => '150px'
        
));
        ....

This gives me a list of category ids in a new column but the filter doesn’t work.
How can I fix the code so the filter will work?
Can I put the category names instead of ids? I think I need to join with the category varchar table but don’t know how.

The code above is just from trial and error. I don’t understand why in addExpressionAttributeToSelect the last argument need to be category_ids and if I change it to something else it fails.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

Explore this module:
http://www.magentocommerce.com/magento-connect/WDCA/extension/748/enhanced-product-grid
will help you to know how to customize admin product grids.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
rrroulio
Sr. Member
 
Avatar
Total Posts:  88
Joined:  2008-03-26
france moselle
 

MagentoPycho, this module don’t show category filter but it is very useful to filter by attributes !

kdecom-magento.com, Great idea to change id’s to name. i wait it !!
But i suppose it works with normal grid and not enhanced grid module?Since 1.4, when you install this modulet, normal grid product list is desactivate. Is it possible to make a clas to include in this module?

 
Magento Community Magento Community
Magento Community
Magento Community
 
misthero
Jr. Member
 
Total Posts:  1
Joined:  2010-08-15
 

i found this to work on latest version of magento:

The first thing we need to do is to tell magento that we’d like to add a category filter to product grid just like any other attributes. So in the file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php, add the following line to _prepareCollection function:

->addAttributeToSelect('category_ids')

You can either call this method in a function chain or use it directly to the product collection $collection.

This is to tell magento that category_ids is a product attribute that later we want the product list to filter by.

After that, we need to visually display this category filter column on the front end. In the same Grid.php file, add the code snippet below into _prepareColumns function:

$categories Mage::getResourceModel('catalog/category_collection')
->
addFieldToFilter(array(array('attribute'=>'level','nin'=>array('0','1'))));
$category_sets = array();
foreach(
$categories as $category){
$id 
$category->getEntityId();
$cat_mod Mage::getModel('catalog/category')->load($id);
$category_sets[$id] $cat_mod->getName();
}
$this
->addColumn('category_ids',
array(
'header'=> Mage::helper('catalog')->__('Categories'),
'index' => 'category_ids',
'width' => '100px',
'type' => 'options',
'options' => $category_sets
));

To add a dropdown type of filter, we need to specify type=>options and options=>$sets when calling addColumn on the grid object. $sets is an array of which elements should be in the format id=>value. The above code snippet use categories that are not super category and root categories [addFieldToFilter(array(array(’attribute’=>’level’,’nin’=>array(’0’,’1’)))].

If you refresh your product admin page right now, you should be able to see that category filter column shows. But it’s only a beginning. There’s no corresponding category value in the column for any product!

To accomplish that, we will need to find the right place to have category values rendered for products. If you see grid’s template grid.phtml and look for the place where column values are displayed, you would guess that in the file app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Options.php, function render is being called.

Within this function, getData on the product object is called. That’s why we don’t get category ids because category_ids isn’t actually a product field. Replace the original line:

$value $row->getData($this->getColumn()->getIndex());

With the following:

$attr $this->getColumn()->getIndex();
if(
$attr == 'category_ids'){
$value 
$row->getCategoryIds($row);
}else{
$value 
$row->getData($attr);
}

Refresh the page again, we can see category values displayed in the column. Now comes the most important part: adding category filter logic. Why are we doing this?  When we call $collection->addAttributeToSelect(’*’) and print all column names in this function, we can see category_ids is listed here but it’s not actually a product field. So if corresponding logic is not added for this field and we try to use the category filter, we shoul be able to see the error output on the page.

Patiently trace down how product grid is rendered, you would find that in app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php,
product collection is filtered in the function addAttributeToFilter. Add an elseif block in the last if-else block.

elseif(is_string($attribute) && $attribute == 'category_ids'){
if(isset($condition['eq'])){
$this
->getSelect()->join(
array(
'category' => $this->getTable('catalog/category_product')),
'category.product_id=e.entity_id AND category.category_id='.$condition['eq'],
array()
);
}
return $this;
}

This is to filter products with the exact same category id with the category filter request.

Last but not least, add this statement block to addAttributeToSort function

if ($attribute == 'category_ids'{
$this
->getSelect()->order('e.entity_id ' $dir);
return 
$this;
}

This is to perform an action when you click on the filter header to make a descent/ascendent sorting. I only give a sorting method as the id sorter here.

this is not my code, i found it here: http://kathytong.blogspot.it/2010/09/magento-how-to-add-category-filter-to.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
jtechextensions
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2013-01-06
 

here is a good solid solution

http://www.magentocommerce.com/magento-connect/catalog/product/view/id/15880/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Amasty
Mentor
 
Avatar
Total Posts:  3988
Joined:  2009-11-10
 
pablop - 13 August 2010 12:43 AM

I’m trying to add a column with a list of categories to the admin product grid.

Hello, as an effective solution you may also try this extension: http://amasty.com/extended-product-grid-with-editor.html. It allows adding any product attributes to the grid, display product thumbnails and categories.

With the module you can also easily filter and sort your products.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top