How to Display Option Text for Dropdown Attributes in Magento Product Report Grid
As an experienced developer, I understand that customizing reports in Magento requires modifying core files, which can be daunting for beginners. In this post, I’ll guide you through displaying the option text instead of values for dropdown attributes like ‘Color’ and ‘Size’ in the Reports->Products Ordered grid.
Understanding the Problem
The default _prepareColumns() method in /app/code/core/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php doesn’t fetch attribute options from the database. Instead, it uses hardcoded values for dropdown attributes, leading to incorrect display of option text.
To fix this, we need to:
- Fetch the required options from the database.
- Create an array mapping option IDs to their corresponding option texts.
- Use this array in the column definition for the desired attribute.
Step-by-Step Solution
1. Prepare the Collection with Required Attributes
In your localized Grid.php file (e.g., /app/code/local/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php), override the _prepareCollection() method:
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('color', 'size')); // Add required attributes
parent::_prepareCollection();
$this->getCollection()
->initReport('reports/product_sold_collection');
return $this;
}
2. Fetch Attribute Options and Create Mapping Array
Still in the localized Grid.php file, modify the _prepareColumns() method as follows:
protected function _prepareColumns()
{
// Fetch color options from database
$colors = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter(80) // Replace with your 'Color' attribute ID
->setStoreFilter()
->load()
->toOptionHash('option_id', 'value');
// Fetch size options from database (similar to colors)
$sizes = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter(81) // Replace with your 'Size' attribute ID
->setStoreFilter()
->load()
->toOptionHash('option_id', 'value');
// Existing column definitions...
$this->addColumn('color', array(
'header' => Mage::helper('reports')->__('Color'),
'index' => 'color',
'type' => 'options',
'options' => $colors
));
$this->addColumn('size', array(
'header' => Mage::helper('reports')->__('Size'),
'index' => 'size',
'type' => 'options',
'options' => $sizes
));
}
Conclusion
By following these steps and properly modifying the Grid.php file, you have now successfully displayed option text instead of values for dropdown attributes in your Magento report grid.
Remember, when working with Magento core files:
- Always create a localized copy (app/code/local) to avoid conflicts during updates.
- Be cautious while overriding methods; ensure you understand their functionality and dependencies.
- Test changes thoroughly to prevent any unwanted side effects on the store’s performance or user experience.
If you found this solution helpful, please share it with your colleagues. For more Magento development tips and best practices, visit Laravel Company.
Happy coding!