How to extend Magento Order Grid?

How to extend Magento Order Grid?

How to extend Magento Order Grid?

Here is small example which explains how to modify order grid. Main class for order grid is “Mage_Adminhtml_Block_Sales_Order_Grid”, if you want to add some column you have to rewrite this class (block).

How to rewrite magento block:

1
2
3
4
5
6
7
< blocks >
< adminhtml >
< rewrite >
< sales_order_grid >Inchoo_Test_Block_Adminhtml_Order_Grid</ sales_order_grid >
</ rewrite >
</ adminhtml >
</ blocks >

If you call block “adminhtml/sales_order_grid”, you will get Inchoo_Test_Block_Adminhtml_Order_Grid of course you need to create your class Inchoo_Test_Block_Adminhtml_Order_Grid.

This is example of our block Inchoo_Test_Block_Adminhtml_Order_Grid:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
< ?php
class Inchoo_Test_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
      public function __construct()
     {
         parent::__construct();
         $this ->setId( 'sales_order_grid' );
         $this ->setUseAjax(true);
         $this ->setDefaultSort( 'created_at' );
         $this ->setDefaultDir( 'DESC' );
         $this ->setSaveParametersInSession(true);
     }
 
     /**
      * Retrieve collection class
      *
      * @return string
      */
     protected function _getCollectionClass()
     {
         return 'sales/order_grid_collection' ;
     }
 
     protected function _prepareCollection()
     {
         $collection = Mage::getResourceModel( $this ->_getCollectionClass());
 
         //we changed mysql query, we added inner join to order item table
         $collection ->join( 'sales/order_item' , 'order_id=entity_id' , array ( 'name' => 'name' , 'sku' => 'sku' , 'qty_ordered' => 'qty_ordered' ), null, 'left' );
         $this ->setCollection( $collection );
         return parent::_prepareCollection();
     }
 
     protected function _prepareColumns()
     {
 
         $this ->addColumn( 'real_order_id' , array (
             'header' => Mage::helper( 'sales' )->__( 'Order #' ),
             'width' => '80px' ,
             'type'  => 'text' ,
             'index' => 'increment_id' ,
         ));
 
         if (!Mage::app()->isSingleStoreMode()) {
             $this ->addColumn( 'store_id' , array (
                 'header'    => Mage::helper( 'sales' )->__( 'Purchased from (store)' ),
                 'index'     => 'store_id' ,
                 'type'      => 'store' ,
                 'store_view' => true,
                 'display_deleted' => true,
                 'filter_index' => 'main_table.store_id'
             ));
         }
 
         $this ->addColumn( 'created_at' , array (
             'header' => Mage::helper( 'sales' )->__( 'Purchased On' ),
             'index' => 'created_at' ,
             'type' => 'datetime' ,
             'width' => '100px' ,
             'filter_index' => 'main_table.created_at'
         ));
 
         $this ->addColumn( 'billing_name' , array (
             'header' => Mage::helper( 'sales' )->__( 'Bill to Name' ),
             'index' => 'billing_name' ,
         ));
 
         $this ->addColumn( 'qty_ordered' , array (
             'header'    => Mage::helper( 'sales' )->__( 'Items Ordered' ),
             'index'     => 'qty_ordered' ,
             'type'      => 'number' ,
             'total'     => 'sum'
         ));
 
         $this ->addColumn( 'sku' , array (
             'header'    => Mage::helper( 'catalog' )->__( 'SKU' ),
             'index'     => 'sku' ,
             'type' => 'text'
         ));
 
         $this ->addColumn( 'base_grand_total' , array (
             'header' => Mage::helper( 'sales' )->__( 'G.T. (Base)' ),
             'index' => 'base_grand_total' ,
             'type'  => 'currency' ,
             'currency' => 'base_currency_code' ,
         ));
 
         $this ->addColumn( 'grand_total' , array (
             'header' => Mage::helper( 'sales' )->__( 'G.T. (Purchased)' ),
             'index' => 'grand_total' ,
             'type'  => 'currency' ,
             'currency' => 'order_currency_code' ,
         ));
 
         $this ->addColumn( 'status' , array (
             'header' => Mage::helper( 'sales' )->__( 'Status' ),
             'index' => 'status' ,
             'type'  => 'options' ,
             'width' => '70px' ,
             'options' => Mage::getSingleton( 'sales/order_config' )->getStatuses(),
         ));
 
         return $this ;
     }
 
     protected function _prepareMassaction()
     {
         $this ->setMassactionIdField( 'entity_id' );
         $this ->getMassactionBlock()->setFormFieldName( 'order_ids' );
         $this ->getMassactionBlock()->setUseSelectAll(false);
 
         if (Mage::getSingleton( 'admin/session' )->isAllowed( 'sales/order/actions/cancel' )) {
             $this ->getMassactionBlock()->addItem( 'cancel_order' , array (
                  'label' => Mage::helper( 'sales' )->__( 'Cancel' ),
                  'url'  => $this ->getUrl( '*/sales_order/massCancel' ),
             ));
         }
 
         if (Mage::getSingleton( 'admin/session' )->isAllowed( 'sales/order/actions/hold' )) {
             $this ->getMassactionBlock()->addItem( 'hold_order' , array (
                  'label' => Mage::helper( 'sales' )->__( 'Hold' ),
                  'url'  => $this ->getUrl( '*/sales_order/massHold' ),
             ));
         }
 
         if (Mage::getSingleton( 'admin/session' )->isAllowed( 'sales/order/actions/unhold' )) {
             $this ->getMassactionBlock()->addItem( 'unhold_order' , array (
                  'label' => Mage::helper( 'sales' )->__( 'Unhold' ),
                  'url'  => $this ->getUrl( '*/sales_order/massUnhold' ),
             ));
         }
 
         $this ->getMassactionBlock()->addItem( 'pdfinvoices_order' , array (
              'label' => Mage::helper( 'sales' )->__( 'Print Invoices' ),
              'url'  => $this ->getUrl( '*/sales_order/pdfinvoices' ),
         ));
 
         $this ->getMassactionBlock()->addItem( 'pdfshipments_order' , array (
              'label' => Mage::helper( 'sales' )->__( 'Print Packingslips' ),
              'url'  => $this ->getUrl( '*/sales_order/pdfshipments' ),
         ));
 
         $this ->getMassactionBlock()->addItem( 'pdfcreditmemos_order' , array (
              'label' => Mage::helper( 'sales' )->__( 'Print Credit Memos' ),
              'url'  => $this ->getUrl( '*/sales_order/pdfcreditmemos' ),
         ));
 
         $this ->getMassactionBlock()->addItem( 'pdfdocs_order' , array (
              'label' => Mage::helper( 'sales' )->__( 'Print All' ),
              'url'  => $this ->getUrl( '*/sales_order/pdfdocs' ),
         ));
 
         return $this ;
     }
 
     public function getRowUrl( $row )
     {
         if (Mage::getSingleton( 'admin/session' )->isAllowed( 'sales/order/actions/view' )) {
             return $this ->getUrl( '*/sales_order/view' , array ( 'order_id' => $row ->getId()));
         }
         return false;
     }
     public function getGridUrl()
     {
         return $this ->getUrl( '*/*/grid' , array ( '_current' =>true));
     }
}

If you want to add some column you need to add next code in method: _prepareColumns

1
2
3
4
5
$this ->addColumn( 'sku' , array (
'header'    => Mage::helper( 'catalog' )->__( 'SKU' ),
'index'     => 'sku' ,
'type' => 'text'
));

Also you will notice that we modified method: _prepareCollection(), we changed SQL query, we made inner join to second database table in order to get data from second database table. This is our working example and your SQL query you can modify as you wish.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值