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

Page 3 of 3
Adding custom attribute filter in product grid (in Admin)
 
cocos
Jr. Member
 
Total Posts:  24
Joined:  2009-12-11
South Korea, Seoul
 
cooper - 22 October 2008 12:29 PM

Lindy,

You are right : it works! Your observation give me a way.

In fact I had created a local module :

1 - edit my own :  /app/code/local/Mymodule/Adminhtlm/Block/catalog/Product/Grid.php
2 - replace class definition in Grid.php by : Class Mymodule_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
3 - create : /app/etc/modules/Mymodule.xml and /app/etc/local.xml

And the result is as describe in my first post.

So what I have done after your response is to put the code in app/code/local/Mage/Adminhtlm/Block/catalog/Product/Grid.php (with the original class defintion) and eveythings work now.

I am not a developper and I don’t know how to do this with a custom module. Do you have an idea?

I hope my english is comprehensible enough. tongue rolleye

SOLUTION.
Open the file “app/code/local/[YOUR MODULE NAME]/Adminhtlm/Block/catalog/Product/Grid.php” And Replace “3 area” !
For more information, See attachments Image file !
Have a nice day ^^

Image Attachments
STEP1.gifSTEP2.gif
 
Magento Community Magento Community
Magento Community
Magento Community
 
jhaagmans
Jr. Member
 
Total Posts:  14
Joined:  2010-08-04
 

I’m on this at the moment as well. I’m wondering whether something like this could work:

<?php

class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{

    
protected function _prepareCollection()
    
{
    $collection 
parent::_prepareCollection()->getCollection();
        
$collection->addAttributeToSelect('something');
        
// etc

        
$this->setCollection($collection);
        return 
$this
    }
}  ?>
As you can see I’m looking for a way to extend the Grid class without touching or overriding the core code. Any thoughts?
 
Magento Community Magento Community
Magento Community
Magento Community
 
ahaomar
Jr. Member
 
Total Posts:  24
Joined:  2010-07-07
 

Hello i need to add column category name in items list grid (order detail page magento admin) also need to make columns sortable can some one guide me how can i do that please see image

thanks

http://i.stack.imgur.com/Ns2uJ.png

 
Magento Community Magento Community
Magento Community
Magento Community
 
timsttiiup
Jr. Member
 
Total Posts:  6
Joined:  2011-10-02
 

Here is the solution to do this quick and easily.

The reason this seems hard is magento’s objects are slightly incompatible with each other at times.  A hash and an option list are two different things.

Magento’s backend wants you to specify a hash array.  Do the following:

(FYI this should be an admin theme mod, not a hack to core, but i’m using an example that just works without trying to teach theming)

Edit {magedir}/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grip.php
Look for the _prepareCollection() function.  Add an ‘addAttributeToSelect()’ call with your attribute code.  Mine is called vendor_code.

protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('vendor_code')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id');

... 
more code

Now, farther down, find the _prepareColumns() function. You are going to add another $this->addColumn() call, but create a hash array manually instead of asking magento to do it.  You need to get your attribute’s ID and put it in the setAttributeFilter() call. Mine is 554. You can obtain this ID in a variety of ways, but I just typed it in. It should never change for my setup.

protected function _prepareColumns()

        
// Load the values collection
        
$valuesCollection Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->
setStoreFilter(0false)
                ->
setAttributeFilter(554)
                ->
load();

        
// Convert the values collection to a hash table
        
$hash = array();
        foreach (
$valuesCollection->toOptionArray() as $option){
                $value 
$option['value'];
                
$label $option['label'];
                
$hash[$value] $label;
        
}

        
// Add the column
        
$this->addColumn('vendor_code',
            array(
                
'header'=> "Vendor Code",
                
'width' => '100px',
                
'index' => 'vendor_code',
                
'type' => 'options',
                
'options' => $hash,
        ));

.... 
more code

You have to add the ‘type => ‘options’ and then specify its options ($hash) for this to work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Towering Media
Sr. Member
 
Total Posts:  77
Joined:  2011-02-08
Seattle, WA
 

I found this solution to be the most sound and works on the newer versions of magento:

http://prattski.com/2010/03/11/magento-add-manufacturer-column-to-admin-product-grid/

$manufacturer_items = Mage::getModel(’eav/entity_attribute_option’)->getCollection()->setStoreFilter()->join(’attribute’,’attribute.attribute_id=main_table.attribute_id’, ‘attribute_code’);
foreach ($manufacturer_items as $manufacturer_item) :
if ($manufacturer_item->getAttributeCode() == ‘manufacturer’)
$manufacturer_options[$manufacturer_item->getOptionId()] = $manufacturer_item->getValue();
endforeach;

$this->addColumn(’manufacturer’,
array(
‘header’=> Mage::helper(’catalog’)->__(’Manufacturer’),
‘width’ => ‘100px’,
‘type’ => ‘options’,
‘index’ => ‘manufacturer’,
‘options’ => $manufacturer_options
));

 
Magento Community Magento Community
Magento Community
Magento Community
 
zerium
Jr. Member
 
Total Posts:  1
Joined:  2011-06-10
 

Easiest way to do this:

Add into your custom extension config.xml global->blocks subsection:

<global>
  <
blocks>
    <
adminhtml>
      <
rewrite>
        <
catalog_product_grid>Extension_Submodule_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
      </
rewrite>
     </
adminhtml>
  </
blocks>
</global>

now your Extension>Submodule>Adminhtml>Block>Catalog>Product>Grid.php file:

<?php

class Mypackage_Mymodule_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

  
public function setCollection($collection){
    $collection
->addAttributeToSelect('my_product_attribute');                    
    
parent::setCollection($collection);
  
}

  
protected function _prepareColumns(){
    $this
->addColumnAfter('my_product_attribute'
                      array(
'header'=> Mage::helper('catalog')->__('My Product Attribute'),
                            
'width' => '70px',
                            
'index' => 'my_product_attribute',
                            
'type'  => 'options',
                            
'options' => array('0' => 'No''1' => 'Yes'))
                    , 
'name');
    
parent::_prepareColumns();
    return 
$this;
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
mikaelmattsson
Jr. Member
 
Total Posts:  1
Joined:  2012-11-27
 
zerium - 17 September 2013 09:49 AM

Easiest way to do this:
...

Thanks, this helped me.

Here is my version to list manufacturers:

class Mypackage_Mymodule_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

    
public function setCollection($collection){
        $collection
->addAttributeToSelect('manufacturer');                    
        
parent::setCollection($collection);
    
}

    
protected function _prepareColumns(){
        $this
->addColumnAfter('manufacturer'
            array(
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'width' => '70px',
                
'index' => 'manufacturer',
                
'type'  => 'options',
                
'options' => $this->_getAttributeOptions('manufacturer'))
            , 
'name');
        
parent::_prepareColumns();
        return 
$this;
    
}

    
protected function _getAttributeOptions($attribute_code){
        $attribute 
Mage::getModel('eav/config')->getAttribute('catalog_product'$attribute_code);
        
$options = array();
        foreach( 
$attribute->getSource()->getAllOptions(truetrue) as $option {
            $options[$option[
'value']] $option['label'];
        
}
        
return $options;
    
}


}
 
Magento Community Magento Community
Magento Community
Magento Community
 
cghisi
Jr. Member
 
Total Posts:  2
Joined:  2014-01-22
 

Hi,

I have used your solution for create an additionnal column on the product datagrid. Your solution work great, my new column appears but i have a problem when I use filters.

When i search by name in the datagrid filter, the content of my column disappears. Do you have an idea ? Do you have the same problem ?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
keyur92
Jr. Member
 
Total Posts:  1
Joined:  2013-06-27
 

@zerium

Thank you very much it works for me

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 3 of 3