magento add custom column to order grid

11 篇文章 0 订阅
Add sales_flat_order.order_uuid column example:

We are going to first copy the core file to a local directory, then code a bit.
Copy /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php. First, to make custom attribute available for inserting to grid we need to modify _prepareCollection function.

Here, we are working with database table fields and using SQL joins. Let`s suppose that we need to add a order_uuid column. Search for_prepareCollection function and simply add this line:

 $collection->getSelect()
        	->joinLeft(
        		"sales_flat_order",
        		"main_table.increment_id = sales_flat_order.increment_id",
                array('order_uuid'=>'sales_flat_order.order_uuid')
            );
Before
 $this->setCollection($collection);
 return parent::_prepareCollection();
Then,importantly,change
 return parent::_prepareCollection();
to
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();

so function will look like:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()
        	->joinLeft(
        		"sales_flat_order",
        		"main_table.increment_id = sales_flat_order.increment_id",
                array('order_uuid'=>'sales_flat_order.order_uuid')
            );
        //Mage::log($collection->getSelect()->__toString());
        $this->setCollection($collection);
        return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }

As you can see, we have joined the sales_flat_ordertable that stores theorder_uuid field.

To insert this column into the order grid, edit _prepareColumns() function. Here you can see how default columns are inserted, so using the addColumn method, we add our own order_uuid column.

 /* add a colomn */
        $this->addColumn('order_uuid', array(
            'header'=> Mage::helper('sales')->__('Order Uuid'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'order_uuid',
            'sortable'  => true,
            'is_system' => false,
 ));
That’s it.


Similarly, for removing a field, you just need to delete the corresponding portion of code.
A few more advanced examples are provided below.

Making a new custom Address column that consists of country, city and street:

$collection->getSelect()->join(
      'sales_flat_order_address', 
      'main_table.entity_id = sales_flat_order_address.parent_id',
      array('telephone',
            'postcode',
            'address'=>new Zend_Db_Expr('concat(sales_flat_order_address.country_id, 
            ", ",
            sales_flat_order_address.city,
             ", ",
            sales_flat_order_address.street)')) 
);
Product SKUs of an order:
$collection->getSelect()->join(
      'sales_flat_order_item', 
      '`sales_flat_order_item`.order_id=`main_table`.entity_id', 
      array('skus'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")'))
);
The last, is an example of adding custom product attributes. It stores at  catalog_product_entity_varchar  table and we need to know its  attribute_id :
$collection->getSelect()->join(
      'sales_flat_order_item', 
      '`sales_flat_order_item`.order_id=`main_table`.entity_id AND `sales_flat_order_item`.parent_item_id IS NULL  ',
       null
);
$collection->getSelect()->join(
      'catalog_product_entity_varchar',
      '`catalog_product_entity_varchar`.attribute_id=144 AND 
       `catalog_product_entity_varchar`.entity_id = `sales_flat_order_item`.`product_id`', 
      array('models'  => new Zend_Db_Expr('group_concat(`catalog_product_entity_varchar`.value SEPARATOR ",")'))
);

Remember to look at the database and see which tables store the necessary attributes.
I hope this article was helpful for you!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值