2013-07-26 148 views
7

我試圖將自定義列添加到銷售訂單網格,並且已將連接和列添加到集合,但該列顯示爲空。此外,當我嘗試過濾時,出現此錯誤:未找到列:1054未知列'方法'中'where子句'。我的代碼似乎與大多數教程中的代碼相同,因此我無法弄清楚爲什麼我的專欄沒有任何付款數據。我回應了最終的SQL查詢,並在MySQL Workbench中運行它,結果和語法似乎沒有問題。我錯過了什麼嗎?我是否需要將一列添加到sales_flat_order_grid表中?我意識到我的「漂亮」付款方式名稱可能與我在過濾時在列中顯示的付款代碼無法良好匹配,但在過濾器選項中使用付款代碼時,我遇到同樣的問題。我想我會在獲得一些列數據後再打這個細節。我正在使用Enterprise 1.12.0.2。Magento將付款方式添加到管理訂單網格

應用程序/代碼/本地/ mymodule中/ adminhtml /塊/銷售/順序/格:

 protected function _prepareCollection() 
{ 
    $collection = Mage::getResourceModel($this->_getCollectionClass()); 
    $collection->getSelect()->joinLeft(array('sfop'=>'sales_flat_order_payment'), 'main_table.entity_id = sfop.parent_id',array('sfop.method')); 
    $this->setCollection($collection); 
    return parent::_prepareCollection(); 
} 

    protected function _prepareColumns() 
{ //edited for brevity...only my additions below![enter image description here][1] 
     $payments = Mage::getSingleton('payment/config')->getActiveMethods(); 
    $methods = array(); 
    foreach ($payments as $paymentCode=>$paymentModel) 
    { 
     $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title'); 
     $methods[$paymentCode] = $paymentTitle; 
    } 

    $this->addColumn('method', array(
     'header' => Mage::helper('sales')->__('Payment Method'), 
     'index' => 'method', 
     'filter_index' => 'sfop.method', 
     'type' => 'options', 
     'width' => '70px', 
     'options' => $methods, 
    )); 

} 

Admin Screen

回答

7

您需要返回管理網格的父類的不是父你正在改寫。

改爲如下方法_prepareCollection(): - 中提出

protected function _prepareCollection() 
{ 
    $collection = Mage::getResourceModel($this->_getCollectionClass()); 
    $collection->join(array('payment'=>'sales/order_payment'),'main_table.entity_id=parent_id','method'); 
    $this->setCollection($collection); 
    return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(); 
} 
+1

謝謝合作!雖然我確實得到了SQL錯誤「ambiguous column parent_id」,所以我必須將'main_table.entity_id = parent_id'更改爲'main_table.entity_id = payment.parent_id'。此外,由於您已將表格別名從「sfop」更改爲「付款」,因此您必須在調用addColumn()時將相應的更改變爲「filter_index」選項。 –

1
$collection->getSelect()->joinLeft('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method')); 
$this->addColumn('method', array(
     'header' => Mage::helper('sales')->__('Method'), 
     'index' => 'method', 
     'filter_index' => 'sales_flat_order_payment.method', 
     )); 
+0

它不起作用,您需要添加:'filter_index'=>'payment.method' - > payment是我們在連接中指定的表的別名。 – jruzafa

2

解決辦法:

http://www.atwix.com/magento/column-to-orders-grid/

是更清潔。

無論如何,如果在網站範圍上啓用了付款方式,那麼檢索$方法作爲後端網格的選項的方式不是多層就緒。在這種情況下,這樣做:

 $methods = array(); 
     foreach (Mage::app()->getStores() as $storeId => $store) { 
     $payments = Mage::getSingleton('payment/config')->getActiveMethods($storeId); 

     foreach ($payments as $paymentCode => $paymentModel) { 
      $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title'); 
      if (!isset($methods[$paymentCode])) { 
       $methods[$paymentCode] = $paymentTitle; 
      } 
     } 
    } 
1

對於渲染,並不是所有的付款方式與標題(TIG PostNL例如)

$payments = Mage::getSingleton('payment/config')->getActiveMethods(); 
$methods = array(); 
foreach ($payments as $paymentCode=>$paymentModel) 
{ 
    $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title', 1); 
    if(empty($paymentTitle)) { 
     $paymentTitle = Mage::helper('payment')->getMethodInstance($paymentCode)->getTitle(); 
    } 
    $methods[$paymentCode] = $paymentTitle; 
} 
相關問題