1. 程式人生 > >Magento後臺訂單列表頁,增加SKU、Qty、客戶郵箱欄位

Magento後臺訂單列表頁,增加SKU、Qty、客戶郵箱欄位

注意:這裡修改之後分頁和總記錄數被破壞,還需要進一步優化(如果查詢客戶郵箱就不會出現這樣的問題,目前還沒找到解決方案)

修改前:


修改後:


#開啟
\app\code\core\Mage\Adminhtml\Block\Sales\Order\Grid.php
#找到 protected function _prepareCollection()方法,大約57行

新增關聯查詢:

$collection->getSelect()
->join('customer_entity','main_table.customer_id = customer_entity.entity_id', 
		array('customer_email' => 'email')) //客戶email
->join('sales_flat_order_item','main_table.entity_id = sales_flat_order_item.order_id',
		array('qty_ordered' => new Zend_Db_Expr('group_concat( `sales_flat_order_item`.qty_ordered SEPARATOR ", ")'),//Qty
               'sku'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")'//SKU
			   )
         ));
最後的該方法如下:
protected function _prepareCollection()
{
	$collection = Mage::getResourceModel($this->_getCollectionClass());
	//關聯查詢
	$collection->getSelect()->join('customer_entity','main_table.customer_id = customer_entity.entity_id', array('customer_name' => 'email'))
	->join('sales_flat_order_item','main_table.entity_id = sales_flat_order_item.order_id', array(
				'qty_ordered' => new Zend_Db_Expr('group_concat(
					 `sales_flat_order_item`.qty_ordered SEPARATOR ", ")'),
				'sku'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')
				));
	//分組
	$collection->getSelect()->group('main_table.entity_id');//注意這句不可少                          
	$this->setCollection($collection);
	return parent::_prepareCollection();

}		
接下來,
#找到 protected function _prepareColumns()方法,大約84行	 

新增程式碼如下:

$this->addColumn('sku', array(
            'header'    => Mage::helper('catalog')->__('SKU'),
            'index'     => 'sku',
            'width' => '70px',
            'type' => 'text'
        ));
$this->addColumn('qty', array(
	'header' => Mage::helper('sales')->__('Qty'),
	'index' => 'qty_ordered',
	'width' => '70px'
));
$this->addColumn('Email', array(
	'header' => Mage::helper('Sales')->__('Email'),
	'width' => '100px',
	'index' => 'customer_mail',
	'type' => 'text'
));

其他:如果報“1052 Column increment_id in where clause is ambiguous”錯誤。注意修改以下程式碼:
 $this->addColumn('real_order_id', array(
            'header'=> Mage::helper('sales')->__('Order #'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'increment_id',
            'filter_index' =>'main_table.increment_id'//// 這個引數將會解決上述問題  
        ));

如果沒有及時看到效果,需要Compilation一下