2

I'm trying to add new product attributes to Reports->Products Ordered grid. I copied the /app/code/core/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php to /app/code/local/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php and add attributes like this:

protected function _prepareColumns()
    {
        $this->addColumn('created_at', array(
            'header'    =>Mage::helper('reports')->__('Create At'),
            'index'     =>'created_at'
        ));
        $this->addColumn('sku', array(
            'header'    =>Mage::helper('reports')->__('sku'),
            'index'     =>'sku'
        ));
        $this->addColumn('name', array(
            'header'    =>Mage::helper('reports')->__('Product Name'),
            'index'     =>'name'
        ));
        $this->addColumn('color', array(
            'header'    =>Mage::helper('reports')->__('Color'),
            'index'     =>'color',
            'type'      => 'options'
        ));
        $this->addColumn('size', array(
            'header'    =>Mage::helper('reports')->__('Size'),
            'index'     =>'size'
        ));
        $this->addColumn('price', array(
            'header'    =>Mage::helper('reports')->__('Price'),
            'width'     =>'120px',
            'type'      =>'currency',
            'currency_code' => $this->getCurrentCurrencyCode(),
            'index'     =>'price'
        ));

        $this->addColumn('ordered_qty', array(
            'header'    =>Mage::helper('reports')->__('Quantity Ordered'),
            'width'     =>'120px',
            'align'     =>'right',
            'index'     =>'ordered_qty',
            'total'     =>'sum',
            'type'      =>'number'
        ));

        $this->addExportType('*/*/exportSoldCsv', Mage::helper('reports')->__('CSV'));
        $this->addExportType('*/*/exportSoldExcel', Mage::helper('reports')->__('Excel'));

        return parent::_prepareColumns();
    }

Problem is, 'Color' and 'Size' are dropdown attributes thats why they are showing the option values instead of option text. How to show the Text value for dropdown attributes in grid?

EDIT 1

Thanks BOOMER... I have changed my Grid.php as you suggested and it showing the blank color column now. Heres what I did:

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('color');

        parent::_prepareCollection();
        $this->getCollection()
            ->initReport('reports/product_sold_collection');
        return $this;
    }

    /**
     * Prepare Grid columns
     *
     * @return Mage_Adminhtml_Block_Report_Product_Sold_Grid
     */
    protected function _prepareColumns()
    {
        $colors = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter(80) // set your attribute ID here
            ->setStoreFilter()
            ->load()
            ->toOptionHash('option_id', 'value');

        $this->addColumn('created_at', array(
            'header'    =>Mage::helper('reports')->__('Create At'),
            'index'     =>'created_at'
        ));
        $this->addColumn('sku', array(
            'header'    =>Mage::helper('reports')->__('sku'),
            'index'     =>'sku'
        ));
        $this->addColumn('name', array(
            'header'    =>Mage::helper('reports')->__('Product Name'),
            'index'     =>'name'
        ));
        $this->addColumn('color', array(
            'header'    =>Mage::helper('reports')->__('Color'),
            'index'     =>'color',
            'type'      => 'options',
            'options' => $colors
        ));
}
nlegall
  • 32
  • 9
Hum
  • 531
  • 2
  • 12
  • 30
  • More than likely your collection doesn't have the data for the attribute. I would `zend_debug::dump($colors);` and `zend_debug::dump($collection);` to ensure the data is there. – B00MER May 14 '12 at 16:27

2 Answers2

3

First ensure you are adding the attribute to select in your _prepareCollection() method, for example:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('color');

And within your _prepareColumns() you'll need to define the collection of options to use:

$colors = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter(15) // set your attribute ID here
            ->setStoreFilter()
            ->load()
            ->toOptionHash('option_id', 'value');

Be sure and set the attribute ID accordingly. Then use this collection list in your addColumns like so:

$this->addColumn('color', array(
            'header'    =>Mage::helper('reports')->__('Color'),
            'index'     =>'color',
            'type'      => 'options',
            'options' => $colors
        ));

Hope this helps!

B00MER
  • 5,471
  • 1
  • 24
  • 41
  • Thanks for response but color column is showing blank now. I have made the changes as you suggested, please refer to 'EDIT 1' in question. – Hum May 01 '12 at 11:32
0

I would suggest you to use a different approach, at least for custom attributes. Try replacing your $colors = actions with the following

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
$colors = array();
foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
   $colors[$option['value']] = $option['label'];
}

I usually replace the statement inside the foreach with an if condition to avoid duplicate blank options, i.e.:

if ($option['label'])
    $sets[$option['value']] = $option['label'];
Fra H
  • 345
  • 1
  • 5
  • 20