Call-back icon  Questions? Contact our sales team to a request a quote

Magento

eCommerce Software for Online Growth

Magento Forum

   
Adding custom product grid column/filter - trouble getting attribute values
 
petemcw
Jr. Member
 
Total Posts:  19
Joined:  2007-08-31
 

I am trying to add custom columns to the product grid within the Adminhtml module. I want to add columns that display custom product attributes and obviously filter the product grid by those attribute values. I have been successful in adding a column/filter when the attribute is a “Text Field”.  I have another attribute that is a “Dropdown”. This is where I’m having trouble.

I have an attribute called “tv_make” that is of type “Dropdown” and has a few attribute values defined: “Sony”, “Toshiba”, etc. I have been able to add the custom column to the grid, but the values displayed are all ID values like: 13, 12, etc.

The core column “Attribute Set” which has code something like this:

$sets Mage::getResourceModel('eav/entity_attribute_set_collection')
    ->
setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
    ->
load()
    ->
toOptionHash();

$this->addColumn('set_name',
    array(
        
'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
        
'width' => '100px',
        
'index' => 'attribute_set_id',
        
'type'  => 'options',
        
'options' => $sets,
));

Does anyone know how I would replace the code above so that I can get an “OptionHash” of a custom attribute’s defined values? This does not work and I’m not sure what to try next:

$makes Mage::getResourceModel('eav/entity_attribute_collection')
    ->
addAttributeToSelect('tv_model')
    ->
load()
    ->
toOptionHash();

$this->addColumn('make',
    array(
        
'header'=> Mage::helper('catalog')->__('TV Make'),
        
'width' => '80px',
        
'index' => 'tv_make',
        
'type'  => 'options',
        
'options' => $makes,
));

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lee Saferite
Sr. Member
 
Avatar
Total Posts:  251
Joined:  2007-08-31
Lake City, FL
 

Try this:

$attribute Mage::getModel('eav/config')->getAttribute('catalog_product''tv_model');
$options = array();
foreach( 
$attribute->getSource()->getAllOptions(truetrue) as $option {
   $options[$option[
'value']] $option['label'];
}
$this
->addColumn('make',
    array(
        
'header'=> Mage::helper('catalog')->__('TV Make'),
        
'width' => '80px',
        
'index' => 'tv_make',
        
'type'  => 'options',
        
'options' => $options,
));

EDIT: Fixing bugs

 
Magento Community Magento Community
Magento Community
Magento Community
 
petemcw
Jr. Member
 
Total Posts:  19
Joined:  2007-08-31
 

Success!  I had to add in a quick check for empty values, but it certainly worked - thank you!

One more question, for you or anyone out there.  How do you go about figuring out this solution?  I’m not sure where to even go to find the right model, resource, collection, etc.!  Not only that but the method chaining, how do you know which are the right methods to call? I am pretty confident that it would have taken forever to arrive at the above solution if left up to simple trial and error.

So any tips for the novice/intermediate Magento developers out there?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lee Saferite
Sr. Member
 
Avatar
Total Posts:  251
Joined:  2007-08-31
Lake City, FL
 

Well, the short answer is tracing the code path.

The long answer is, I started with the thought, ‘how does Magento do this?’

tracked down these files
/magento/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php
/magento/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Attributes.php

which led here
/magento/app/code/core/Mage/Adminhtml/Block/Widget/Form.php:98

which led here
/magento/lib/Varien/Data/Form/Element/Fieldset.php:101
/magento/lib/Varien/Data/Form/Abstract.php:114
/magento/lib/Varien/Data/Form/Element/Select.php

and, luckily enough, to this line
/magento/app/code/core/Mage/Adminhtml/Block/Widget/Form.php:123

That set me off on another search for the $attribute object

Started here
/magento/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php:60
then
/magento/app/code/core/Mage/Catalog/Model/Product.php:265
then
/magento/app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php:134
then
/magento/app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php:93
then
/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php:441
then
/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php:326
then
/magento/app/code/core/Mage/Eav/Model/Config.php:231
then
/magento/app/code/core/Mage/Eav/Model/Entity/Attribute/Option.php
and
/magento/app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php

Whew.  Anyway.  A good IDE is a must when tracking down things like this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
petemcw
Jr. Member
 
Total Posts:  19
Joined:  2007-08-31
 

Whew, is right!  I appreciate the time it took both to help with the solution and the explanation.  I would agree on the IDE.  I am currently swapping between Eclipse PDT and Zend Studio for Eclipse trial.  They both seem to be pretty decent editors.  Thanks again!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
60008 users|780 users currently online|118863 forum posts