Question

how to add attribute column in Sales Report? I use the Advanced Report of AheadWorks and I've tried to make the attribute column but do not know how to join the sales table with the attribute table. I've show the contents of the attributes (the attributes I was Brand) in drop down style. The following is how I add the attribute column (file location : app\code\local\AW\Advancedreports\Block\Advanced\Sales\Grid.php ) :

http://i.imgur.com/eh7q9PD.png ( image )

protected function _prepareColumns()
{
   .....

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

    $this->addColumn('brand',
        array(
            'header'=> Mage::helper('catalog')->__('Brand'),
            'width' => '80px',
            'index' => 'brand',
            'type'  => 'options',
            'options' => $options,
    )); 

   ........

           return parent::_prepareColumns();
}

add : November 07, 2014

(sorry, I do not know in which section to put this. edit my question or add as an answer)

I add this subquery to $collection

//CREATE A SUBQUERY FOR GETTING A BRAND - Start
$brandTable = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('entity_id');
$brandTable->getSelect()->join( array('cpei' => 'catalog_product_entity_int'),
                                "e.entity_id = cpei.entity_id",
                                array()
                                );
$brandTable->getSelect()->join( array('eaov' => 'eav_attribute_option_value'),
                                "cpei.value = eaov.option_id",
                                array('brand' => 'value')
                                );
$brandTable->getSelect()->join( array('eao' => 'eav_attribute_option'),
                                "eaov.option_id = eao.option_id",
                                array()
                                );
$brandTable->getSelect()->join( array('ea' => 'eav_attribute'),
                                "eao.attribute_id = ea.attribute_id AND ea.attribute_code = 'brand'",
                                array()
                                );
$collection->getSelect()->join( array('child_brand' => new Zend_Db_Expr( '(' . $brandTable->getSelect() . ')')),
                                "child_brand.entity_id = _product.entity_id",
                                array('brand'=>'child_brand.brand')
                                );
//CREATE A SUBQUERY FOR GETTING A BRAND - End
Was it helpful?

Solution

In addition to preparing columns, you need to prepare the collection. Take a look at the _prepareCollection() method and add in your join(s)

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('advancedreport/advancedreport_collection')
        ->addInfo();
    $collection->join(
        'table2',
        'table1.field = table2.field',
        array(
            'brand' => 'value'
        ),
        null,
        'inner'
    );
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

OTHER TIPS

I add this query to function _prepareColumns() :

//Adding column Brand
$this->addColumn('brand', array(
    'header' => $this->_helper()->__('Brand'),
    'index' => 'brand',
    'type' => 'text',
    'width' => '100px',
    'ddl_type' => Varien_Db_Ddl_Table::TYPE_VARCHAR,
    'ddl_size' => 255,
    'ddl_options' => array('nullable' => true),
));

you can see complete Grid.php code in here : http://pastebin.com/jghDffvS

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top