How to add Custom Column in sales/order grid in Magento?

雨燕双飞 提交于 2019-12-04 17:53:13

If in your _prepareCollection method I print the query via:

echo $collection->getSelect()->assemble();

I get this:

SELECT 
    `main_table`.*, 
    group_concat(sales_flat_shipment_track.track_number SEPARATOR ",") AS `track_number`, 
    group_concat(sales_flat_shipment_track.title SEPARATOR ",") AS `title` 

FROM `sales_flat_order_grid` AS `main_table` 

INNER JOIN `sales_flat_shipment_track` 
    ON main_table.entity_id = sales_flat_shipment_track.order_id

By this query I will always get a result, even an "empty" row when there are no orders on the table. Rather, I think what you are trying to achieve can be done using subqueries:

SELECT 
    `main_table`.*, 
    (
        SELECT 
            group_concat(`t`.`track_number` SEPARATOR ",") AS `track_number`

        FROM `sales_flat_shipment_track` AS `t`

        WHERE `main_table`.`entity_id` = `t`.`order_id`
    ),
    (
        SELECT 
            group_concat(`t`.`title` SEPARATOR ",") AS `title`

        FROM `sales_flat_shipment_track` as `t`

        WHERE `main_table`.`entity_id` = `t`.`order_id`
    )

FROM `sales_flat_order_grid` AS `main_table`;

So to translate that for Magento, it would look something like this:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('sales/order_grid_collection');

    $collection->getSelect()
        ->from(
            array(),
            array(
                'track_number' => new Zend_Db_Expr('(
                    SELECT GROUP_CONCAT(`t`.`track_number` SEPARATOR ",")
                    FROM `sales_flat_shipment_track` as `t`
                    WHERE `main_table`.`entity_id` = `t`.`order_id`
                )'),
                'title' => new Zend_Db_Expr('(
                    SELECT GROUP_CONCAT(`t`.`title` SEPARATOR ",")
                    FROM `sales_flat_shipment_track` as `t`
                    WHERE `main_table`.`entity_id` = `t`.`order_id`
                )'),
            )
        );

    $this->setCollection($this);

    return parent::_prepareCollection();
}

To your point about the duplicate carrier titles, that is to be expected in a case like this. The only way around it is to add a DISTINCT word in the sub-query for the title, like this:

SELECT GROUP_CONCAT(DISTINCT `t`.`title` SEPARATOR ",")

But I'm not sure what you plan to do with this data in the grid. Hope that helps.

First you're not using the good way to rewrite a Magento file. Take a look a this :

http://inchoo.net/magento/how-to-extend-magento-order-grid/

Once you did it, in Grid.php do

protected function _prepareCollection() {
    $collection = Mage::getResourceModel ( $this->_getCollectionClass () );
    $collection->join(array('so'=>'sales/order'), 'main_table.entity_id=so.entity_id', array('your_coustom_field'=>'your_coustom_field', 'customer_email'=>'customer_email' ), null,'left');
    $this->setCollection ( $collection );
    return parent::_prepareCollection();
}

Then in _prepareColumn method add

$this->addColumn ( 'customer_email', array (
    'header' => Mage::helper ( 'sales' )->__ ( 'customer email' ),
    'index' => 'customer_email'
) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!