2016-10-22 69 views
1

我想在Magento銷售訂單網格中顯示客戶電子郵件。我必須在本地模塊中重寫Mage_Adminhtml_Block_Sales_Order_Grid以添加客戶電子郵件的新列。我得到了每個訂單的電子郵件的價值,但排序和篩選不能按預期工作。銷售訂單網格過濾器和排序在Magento中不起作用

我花了一整天的時間來排序這個問題,但沒有運氣。另外,我也提到了很少的答案。

下面是代碼這是我曾嘗試referring this answer

public function setCollection($collection) 
{ 
    $collection->getSelect()->joinLeft(
     array('sfo'=>'sales_flat_order'), 
     'main_table.entity_id=' . 'sfo' . '.entity_id', 
     array('*') 
    ); 

    $collection->getSelect()->joinLeft(
     array('sfoa'=>'sales_flat_order_address'), 
     'main_table.entity_id=' . 'sfoa' . '.parent_id', 
     array('email') 
    ); 

    $collection->getSelect()->group(array('main_table.entity_id')); 

    $collection->addFilterToMap('increment_id', 'main_table.increment_id'); 

    parent::setCollection($collection); 
} 

protected function _prepareColumns() 
{ 
    $this->addColumnAfter('email', array(
     'header' => Mage::helper('sales')->__('Customer Email'), 
     'width' => '80px', 
     'type' => 'text', 
     'index' => 'email', 
     'filter_index' => 'sfoa.email', 
     'filter_condition_callback' => 'filter_last_login', 
     'order_callback'   => 'sort_last_login', 
    ), 'erp_confirm_order'); 

    return parent::_prepareColumns(); 
} 



public function getGridUrl() 
{ 
    return $this->getUrl('*/*/grid', array('_current'=>true)); 
} 

function filter_last_login($collection, $column) 
{ 
    if (!$column->getFilter()->getCondition()) { 
     return; 
    } 

    $condition = $collection->getConnection() 
     ->prepareSqlCondition('sfoa.email', $column->getFilter()->getCondition()); 
    $collection->getSelect()->where($condition); 
} 

function sort_last_login($collection, $column) 
{ 
    $collection->getSelect()->order($column->getIndex() . ' ' . strtoupper($column->getDir())); 
} 

protected function _setCollectionOrder($column) 
{ 
    if ($column->getOrderCallback()) { 
     call_user_func($column->getOrderCallback(), $this->getCollection(), $column); 

     return $this; 
    } 

    return parent::_setCollectionOrder($column); 
} 

編輯,當我整理&過濾客戶的電子郵件列,而且我得到的錯誤,當我1個

沒有什麼工作點擊默認網格列的其餘部分。

Integrity constraint violation: 1052 Column 'increment_id' in order clause is ambiguous, query was: SELECT `main_table`.*, `sfo`.*, `sfoa`.`email` FROM `sales_flat_order_grid` AS `main_table` 
LEFT JOIN `sales_flat_order` AS `sfo` ON main_table.entity_id=sfo.entity_id 
LEFT JOIN `sales_flat_order_address` AS `sfoa` ON main_table.entity_id=sfoa.parent_id GROUP BY `main_table`.`entity_id` ORDER BY increment_id ASC LIMIT 20 

任何幫助很大的讚賞。 謝謝,

+0

你得到任何錯誤信息或者它根本就什麼都沒有? – Ian

+1

@Ian排序和篩選時沒有任何事情發生。請檢查編輯1.謝謝。 –

回答

1

所以,你面臨的問題不同於原來的答案是你加入了不明確名稱的字段。您需要指定要過濾哪些字段。我自己面對過這個。

當您從sales_flat_order添加列時,您不需要執行自定義篩選或排序回調。只有當您需要一些更復雜的過濾/排序查詢(如上次登錄日期的原始示例)時才需要這樣做。只要確保你的'filter_index'已設置,它會正常工作。以下示例正在從帳單地址添加客戶電子郵件。

class My_Namespace_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid 
{ 
    public function setCollection($collection) 
    { 
     $collection->join(['sfoa' => 'sales/order_address'], 'main_table.entity_id=sfoa.parent_id AND sfoa.address_type="billing"', ['billing_email' => 'sfoa.email']); 

     parent::setCollection($collection); 
    } 

    public function _prepareColumns() 
    { 
     parent::_prepareColumns(); 

     foreach ($this->getColumns() as $column) { 
      if (!$column->getFilterIndex()) { 
       $column->setFilterIndex('main_table.'.$column->getIndex()); 
      } 
     } 

     $this->addColumnAfter('customer_email', [ 
      'header' => Mage::helper('customer')->__('Customer Email'), 
      'width' => '50px', 
      'index' => 'billing_email', 
      'filter_index' => 'sfoa.email', 
     ], 'shipping_name'); 

     $this->sortColumnsByOrder(); 
     return $this; 
    } 
} 

這裏是從訂單表本身就是一個例子:

class My_Namespace_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid 
{ 
    public function setCollection($collection) 
    { 
     $collection->join(['sfo' => 'sales/order'], 'main_table.entity_id=sfo.entity_id', 'customer_email'); 

     parent::setCollection($collection); 
    } 

    public function _prepareColumns() 
    { 
     parent::_prepareColumns(); 

     foreach ($this->getColumns() as $column) { 
      if (!$column->getFilterIndex()) { 
       $column->setFilterIndex('main_table.'.$column->getIndex()); 
      } 
     } 

     $this->addColumnAfter('customer_email', [ 
      'header' => Mage::helper('customer')->__('Customer Email'), 
      'width' => '50px', 
      'index' => 'customer_email', 
      'filter_index' => 'sfo.customer_email', 
     ], 'shipping_name'); 

     $this->sortColumnsByOrder(); 
     return $this; 
    } 
} 
+0

它很棒!非常感謝您的幫助。 –

相關問題