2013-01-25 143 views
2

我遇到了在Magento 1.7.0.0的訂單網格中添加自定義列的問題,我希望你能夠在這裏給我一隻手。 基本上我遵循本指南http://www.atwix.com/magento/customize-orders-grid/,其中解釋了我必須製作/app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php的本地版本,並進行一些更改以獲得所需的額外列。按照上述導向,它說,我不得不編輯功能_prepareCollection()加這行(指定我想在陣列中提取字段)在訂單網格中添加自定義列(Magento 1.7.0.0)

$collection->getSelect()->join('magento_sales_flat_order_address', 'main_table.entity_id = magento_sales_flat_order_address.parent_id',array('telephone', 'email')); 

之前

return parent::_prepareCollection(); 

,並新增兩個在_prepareColumns()像這樣的列:

$this->addColumn('telephone', array(
     'header' => Mage::helper('sales')->__('Telephone'), 
     'index' => 'telephone', 
    )); 

$this->addColumn('email', array(
     'header' => Mage::helper('sales')->__('Email'), 
     'index' => 'email', 
    )); 

,就是這樣,很顯然......或者,也許沒有,因爲當我拋出以下錯誤:

Item (Mage_Sales_Model_Order) with the same id "XXXX" already exist 

到的解決方案,根據下面的評論,是添加以下行_prepareCollection$this->setCollection($collection)

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

加入了這一行後,訂單網格現在顯示的電子郵件和電話列就像我想要的,但事實證明分頁停止工作,它只顯示最近的20個,它說「頁1出1」,「找到2個記錄」在上面。我似乎無法弄清楚爲什麼會發生這種情況,我所看到的每一條評論都沒有超出上面的最後一條指令。什麼可能是這個問題的原因?

我認爲它可以被複制,因爲我沒有對此模型做任何其他修改。

回答

4

好了,解決了這個問題。這就是我所做的: 受這個回答https://stackoverflow.com/a/4219386/2009617的啓發,我製作了一個文件lib/Varien/Data/Collection/Db.php的副本,將它放在app/core/local/Varien/Data/Collection/Db.php下,並複製了在該答案中建議的修改以修復上面給出問題的組選擇計數錯誤。到目前爲止,它似乎工作。

但是,行中出現了一個問題,當我點擊訂單時表示訂單「不再存在」,所以我查看了實際的網址,結果發現url中的order_id與「 order_address表中的「entity_id」,它與訂單的實際關聯ID(parent_id)不一致。經過長時間的MySQL查詢調整後,我意識到問題出在/app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php中的_prepareColumns()getRowUrl()函數所調用的函數中,因爲他們正在檢索錯誤的Id。所以,我做了以下修改:

_prepareColumns(),在代碼中對應的操作列,我改變了「吸氣」「getParentId」,就像這樣:

if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) { 
     $this->addColumn('action', 
      array(
       'header' => Mage::helper('sales')->__('Action'), 
       'width'  => '50px', 
       'type'  => 'action', 
       //~ 'getter'  => 'getId', 
       'getter'  => 'getParentId', 
       'actions' => array(
        array(
         'caption' => Mage::helper('sales')->__('View'), 
         'url'  => array('base'=>'*/sales_order/view'), 
         'field' => 'order_id', 
        ) 
       ), 
       'filter' => false, 
       'sortable' => false, 
       'index'  => 'stores', 
       'is_system' => true, 
     )); 
    } 

而且在在getRowUrl()功能,我改變了這樣的getUrl()函數內的$行語句:

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 $this->getUrl('*/sales_order/view', array('order_id' => $row->getParentId())); 
    } 
    return false; 
} 

而現在它的工作原理像一個魅力。我希望這可以幫助別人。

0

嘗試在addColumn函數使用filter_index:

$this->addColumn('telephone', array(
    'header' => Mage::helper('sales')->__('Telephone'), 
    'index' => 'telephone', 
    'filter_index' => 'tablename.telephone' 
)); 

你可以找出表名與印刷出來的SQL查詢:

var_dump((string)$collection->getSelect()) 
+0

我試過這個,但它沒有做任何不同的事情。但是,我在這裏發現了真正的問題:這是'$ collection-> getSelect() - > group('main_table.entity_id')這行';如果我評論上面的join()查詢並離開group()查詢上,分頁問題發生。它必須是與此相關的東西,儘管它是列填充的唯一方式。 – SevenSonicStructures

3

問題出在查詢中。相反,這個查詢:

$collection->getSelect()->join('magento_sales_flat_order_address', 'main_table.entity_id = magento_sales_flat_order_address.parent_id',array('telephone', 'email')); 

你應該使用這樣的:

$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id AND sales_flat_order_address.address_type = "shipping" ',array('telephone', 'email')); 

在表sales_flat_order_address,PARENT_ID被複制。第一個用於計費,第二個用於運輸。所以你只需要選擇其中一個:計費或運輸。這個值在列地址類型...

相關問題