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 2 of 3
Adding custom attribute filter in product grid (in Admin)
 
wolfdog85
Jr. Member
 
Total Posts:  12
Joined:  2008-09-29
Biloxi, MS, USA
 

I also noticed that it is putting on the page the Notify Low Stock RSS link on there twice when overriding the function. Why is this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
NafeX
Member
 
Total Posts:  47
Joined:  2007-12-18
 

i have the same problem like wolfdog85. overwriting don’t show the manufacturer names and i have 2 Notify Low Stock RSS links…

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

Ok the problem with the overriding is pretty simple, but it’s always the same, it’s easy once you got the solution ...

When you override the grid, you copy protected function _prepareCollection() and doing that you duplicate this line :

parent::_prepareCollection();

So it calls again the _prepareCollection from the core erasing what you have just done with this line

->addAttributeToSelect('custom_attribute');

So in order to make it work you have to replace :

parent::_prepareCollection();

by the code

if ($this->getCollection()) {

            $this
->_preparePage();

            
$columnId $this->getParam($this->getVarNameSort(), $this->_defaultSort);
            
$dir      $this->getParam($this->getVarNameDir(), $this->_defaultDir);
            
$filter   $this->getParam($this->getVarNameFilter(), null);

            if (
is_null($filter)) {
                $filter 
$this->_defaultFilter;
            
}

            
if (is_string($filter)) {
                $data 
= array();
                
$filter base64_decode($filter);
                
parse_str(urldecode($filter), $data);
                
$this->_setFilterValues($data);
            
else if ($filter && is_array($filter)) {
                $this
->_setFilterValues($filter);
            
else if(!== sizeof($this->_defaultFilter)) {
                $this
->_setFilterValues($this->_defaultFilter);
            
}

            
if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
                $dir 
= (strtolower($dir)=='desc') ? 'desc' 'asc';
                
$this->_columns[$columnId]->setDir($dir);
                
$column $this->_columns[$columnId]->getFilterIndex() ?
                    
$this->_columns[$columnId]->getFilterIndex() : $this->_columns[$columnId]->getIndex();
                
$this->getCollection()->setOrder($column $dir);
            
}

            $this
->getCollection()->load();
            
$this->_afterLoadCollection();
        
}

which is the code of the _prepareCollection from Mage_Adminhtml_Block_Widget_Grid that you still need.

Oh and same thing with the rss stuff : replace

return parent::_prepareColumns();
by
return $this;
 
Magento Community Magento Community
Magento Community
Magento Community
 
wolfdog85
Jr. Member
 
Total Posts:  12
Joined:  2008-09-29
Biloxi, MS, USA
 

Works great! Thanks for the research. I will remember to look for those function calls in other areas I work in. Thanks again.

 
Magento Community Magento Community
Magento Community
Magento Community
 
subesh
Sr. Member
 
Avatar
Total Posts:  145
Joined:  2009-01-08
 

THANKS SOMESID THAT REALLY WORKED!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sensi
Sr. Member
 
Total Posts:  103
Joined:  2008-07-31
Paris, France
 
somesid - 30 December 2008 01:35 AM

So in order to make it work you have to replace :

parent::_prepareCollection();


by the code

if ($this->getCollection()) {

            $this
->_preparePage();

            
$columnId $this->getParam($this->getVarNameSort(), $this->_defaultSort);
            
$dir      $this->getParam($this->getVarNameDir(), $this->_defaultDir);
            
$filter   $this->getParam($this->getVarNameFilter(), null);

            if (
is_null($filter)) {
                $filter 
$this->_defaultFilter;
            
}

            
if (is_string($filter)) {
                $data 
= array();
                
$filter base64_decode($filter);
                
parse_str(urldecode($filter), $data);
                
$this->_setFilterValues($data);
            
else if ($filter && is_array($filter)) {
                $this
->_setFilterValues($filter);
            
else if(!== sizeof($this->_defaultFilter)) {
                $this
->_setFilterValues($this->_defaultFilter);
            
}

            
if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
                $dir 
= (strtolower($dir)=='desc') ? 'desc' 'asc';
                
$this->_columns[$columnId]->setDir($dir);
                
$column $this->_columns[$columnId]->getFilterIndex() ?
                    
$this->_columns[$columnId]->getFilterIndex() : $this->_columns[$columnId]->getIndex();
                
$this->getCollection()->setOrder($column $dir);
            
}

            $this
->getCollection()->load();
            
$this->_afterLoadCollection();
        
}

which is the code of the _prepareCollection from Mage_Adminhtml_Block_Widget_Grid that you still need.

There rather replace:

parent::_prepareCollection();

by:

Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
smile
 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

Oh yeah that’s clever :D Thx.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lvanstaden
Jr. Member
 
Total Posts:  10
Joined:  2009-01-25
 

My first post on the forum, hope someone can shed some light here.

I have created two custom modules.
I want to add a new column in the Grid for each of them, but what I have found is that if the one module is active, the other’s new column does not show. Basically they are overriding each other.  Individually they work 100%.

Thus, question is, how do i prevent the second module from stopping the first module’s column from appearing.

All modules are under app/code/local/CompanyName/

In module 1 I have:

config.xml

<blocks>
            <
adminhtml>
                <
rewrite>
                    <
catalog_product_grid>CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                   </
rewrite>
            </
adminhtml>
</
blocks>

CompanyName/ModuleName/Adminhtml/Block/Catalog/Product/Grid.php in method protected function _prepareCollection() I have added ->addAttributeToSelect(’le_special_shipping’)

and method _prepareColumns() looks like this:

protected function _prepareColumns()
    
{
        parent
::_prepareColumns();
        
        
$this->addColumn('shipping',
            array(
                
'header'=> Mage::helper('catalog')->__('Shipping'),
                
'width' => '80px',
                
'index' => 'le_special_shipping',
                
'type'  => 'options',
                
'options' => Mage::getModel('companybasketshipping/product_attribute_source_unit')->getOptionArray(),
        ));
        
        return 
Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(); 
    
}

class is defined as:

Class CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

In module 2 I have basically the same code, it just defines a different column, with it’s own options.
the class define in module 2 is:

Class CompanyName_SecondModule_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

I do not want the modules to depend on each other, as they do different functionality.

To note, one of our other developers ran into a similar issue concerning payment modules, where one module extends checkout/Onepage.php, and if the second payment module is loaded, which also extends Onepage.php, then only the second module seems to be doing it’s thing.

I am sure we are missing something fundemental here, so any pointers appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

Basically the reason it’s doing that is very clear, when you write :

<blocks>
            <
adminhtml>
                <
rewrite>
                    <
catalog_product_grid>CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                   </
rewrite>
            </
adminhtml>
</
blocks>

You’re telling Magento, that the core catalog product grid is now replaced by your new class. If you do that twice, only one will replace the original grid.

Now what I can’t understand, is what you’re trying to do ?

If you want to add several columns to your grid, then do it in the same class, if you want to have two different views of the grid, your doing it wrong.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lvanstaden
Jr. Member
 
Total Posts:  10
Joined:  2009-01-25
 
somesid - 13 March 2009 03:29 AM

Basically the reason it’s doing that is very clear, when you write :

<blocks>
            <
adminhtml>
                <
rewrite>
                    <
catalog_product_grid>CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                   </
rewrite>
            </
adminhtml>
</
blocks>

You’re telling Magento, that the core catalog product grid is now replaced by your new class. If you do that twice, only one will replace the original grid.

Now what I can’t understand, is what you’re trying to do ?

If you want to add several columns to your grid, then do it in the same class, if you want to have two different views of the grid, your doing it wrong.

I want to add two new columns to the standard grid. Each module is to add it’s own new column to the grid.
I can’t do it in one class, as the modules are to run independant, so one could be installed, and not the other.

what do you mean with: ‘if you want to have two different views of the grid’ ?

This post (http://www.magentocommerce.com/boards/viewthread/35361/) describes my issue, however the suggested solution in that post will not work here. (events)

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

That’s a tricky one.
So as I said one thing is sure, if you declare two overload, only one will work, it’s like plain object logic (at least java object logic).

Now, how could you work around your problem maybe, is to declare the two columns in both of your overload and check for the “conditional” columns from (for example) the class existence.

Example in your first module:

$collection->addAttributeToSelect('my_first_attribute');

if(
class_exist('CompanyName_SecondModule_Adminhtml_Block_Catalog_Product_Grid'))
  
$collection->addAttributeToSelect('my_second_attribute');

and in the other module

if(class_exist('CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid'))
    
$collection->addAttributeToSelect('my_first_attribute');

$collection->addAttributeToSelect('my_second_attribute');

Can only work if what you want to produce is symmetrical in the 2 modules. Didn’t try this, it’s just an idea. Not very clean. Can’t think of anything else. What people proposed you with events I’m really skeptical ‘cause there is no event associated to the creation of the grid.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lvanstaden
Jr. Member
 
Total Posts:  10
Joined:  2009-01-25
 
somesid - 13 March 2009 07:35 AM

That’s a tricky one.
So as I said one thing is sure, if you declare two overload, only one will work, it’s like plain object logic (at least java object logic).

I fully agree, was hoping magento had some ‘magical’ way of coping with this issue.

somesid - 13 March 2009 07:35 AM

That’s a tricky one.
Now, how could you work around your problem maybe, is to declare the two columns in both of your overload and check for the “conditional” columns from (for example) the class existence.

Example in your first module:

$collection->addAttributeToSelect('my_first_attribute');

if(
class_exist('CompanyName_SecondModule_Adminhtml_Block_Catalog_Product_Grid'))
  
$collection->addAttributeToSelect('my_second_attribute');

and in the other module

if(class_exist('CompanyName_ModuleName_Adminhtml_Block_Catalog_Product_Grid'))
    
$collection->addAttributeToSelect('my_first_attribute');

$collection->addAttributeToSelect('my_second_attribute');

Can only work if what you want to produce is symmetrical in the 2 modules. Didn’t try this, it’s just an idea. Not very clean.

I was leaning towards the same solution, but in a slightly different way. As we (the company I work at) will be creating quite a few custom modules to allow magento to do ‘some stuff’ that is currently not available in magento (but is in our own web code). These new modules would have some ‘cross-over’ code, like this one where two modules will be wanting to adjust the grid.

The current suggestion (we are discussing our best recourse) is to create some ‘base library modules’ which would contain the code that requires multiple modules to extend (overload) the same core classes. These base library modules would then have the checks to see if the child modules are installed/active, and if so run the required code. This is very similar to your solution, but we then don’t ‘cross-pollute’ the individual module code (code that is independant will stay contained within each module)

So, in this ‘example’ I will end up with three modules:

Module 1 - depends on BaseMyCompany (all other code here, except the grid code)
Module 2 - depends on BaseMyCompany (all other code here, except the grid code)
BaseMyCompany - No deps - includes the grid code, but checks if the other modules are active before it adjusts the grid for each.

This solution ‘sounds’ like it will work.

somesid - 13 March 2009 07:35 AM

Can’t think of anything else. What people proposed you with events I’m really skeptical ‘cause there is no event associated to the creation of the grid.

I again agree.

 
Magento Community Magento Community
Magento Community
Magento Community
 
TimRigg
Jr. Member
 
Total Posts:  9
Joined:  2009-02-26
 

Hi all !

I’m customizing the order grid in the back-end. I would like to display a number in a new column, that corresponds to the sum, for each product of the order, of an attribute I created.

Here is the SQL request that returns me the collection I want:

SELECT `e`.*, SUM(`_table_my_table`.`value`*`_table_product_id`.`qty_ordered`) AS `total`,
            
FROM `sales_order` AS `e

LEFT JOIN `sales_flat_order_item` AS `_table_product_id
    
ON (_table_product_id.order_id=e.entity_id

LEFT JOIN `catalog_product_entity_varchar` AS `_table_ma_table
    
ON (_table_my_table.entity_id _table_product_id.product_id) AND (_table_my_table.attribute_id='493'
    
WHERE (e.entity_type_id '11')

GROUP BY `e`.`entity_id`

But I can’t manage to get that result by using the methods of the collection.

Here is my code:

$collection Mage::getResourceModel('sales/order_collection')

     ->
addAttributeToSelect('*')

     ->
joinField('product_id','sales/order_item','product_id','order_id=entity_id',null'left')

     ->
joinAttribute('mon_attribut''catalog/product''product_id'null'left')

     ->
addExpressionAttributeToSelect'total_my_attribute''SUM({{my_attribute}})*{{qty_ordered}}''my_attribute' ) )

     ->
groupByAttribute('entity_id');

Does anybody know what I do wrong?

Thanks a lot.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Olof
Sr. Member
 
Total Posts:  152
Joined:  2008-10-17
 

Guys, we were dooing this but it just broke in an upg to Magento ver. 1.3.2 has anyone had the same issue?

Rich

 
Magento Community Magento Community
Magento Community
Magento Community
 
Olof
Sr. Member
 
Total Posts:  152
Joined:  2008-10-17
 

Has anyone go this working on newer versions of Magento?

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