在订单网格中添加自定义列(Magento 1.7.0.0) [英] Adding custom columns in order grid (Magento 1.7.0.0)
问题描述
我在Magento 1.7.0.0的订单网格中添加自定义列时遇到了这个问题,我希望您能够在这里为我提供帮助.
基本上,我遵循了本指南 http://www.atwix.com/magento/customize-orders -grid/解释了我必须制作一个本地版本的/app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
并进行一些更改才能拥有我想要的额外列.通过遵循上述指南,它说我必须编辑函数_prepareCollection()
来添加此行(指定要在数组中提取的字段)
I'm having this problem with adding custom columns in the order grid in Magento 1.7.0.0 and I was hoping you'd be able to give me a hand in here.
Basically I followed this guide http://www.atwix.com/magento/customize-orders-grid/ which explained I had to make a local version of /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
and make a couple of changes to have the extra columns I want. By following said guide, it said that I had to edit the function _prepareCollection()
to add this line (specifying the fields I want to extract in the array)
$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()
中添加两列:
And add the two columns in _prepareColumns()
like this:
$this->addColumn('telephone', array(
'header' => Mage::helper('sales')->__('Telephone'),
'index' => 'telephone',
));
$this->addColumn('email', array(
'header' => Mage::helper('sales')->__('Email'),
'index' => 'email',
));
就是这样,显然...也许不是,因为这样做时会引发以下错误:
And that was it, apparently... Or maybe not, since it throws the following error when I do that:
Item (Mage_Sales_Model_Order) with the same id "XXXX" already exist
根据下面的注释,解决方案是在$this->setCollection($collection)
之前的_prepareCollection
中添加以下行:
To which the solution, according to the comments underneath, was to add the following line in _prepareCollection
before $this->setCollection($collection)
:
$collection->getSelect()->group('main_table.entity_id');
添加该行之后,订单网格"现在像我想要的那样显示电子邮件"和电话"列,但是事实证明分页已停止,它仅显示最新的20条,并且显示"顶部1" ,找到2条记录" .我似乎无法弄清楚为什么会这样,而且我看到的每条评论都没有超出上面的最后一条说明.可能是此问题的原因?
After adding the line, the Order Grid now shows the Email and Phone columns just like I want it, but turns out the pagination stopped working, it only shows the most recent 20 and it says "Pages 1 out of 1", "2 records found" on top. I can't seem to figure out why this is happening and every comment I see around doesn't go beyond the last instruction above. What could possibly be the cause of this issue?
由于我没有对该模型进行任何其他修改,因此我认为它可以被复制.
I assume it could be replicated since I haven't made any other modifications of this model.
推荐答案
好的,解决了.这是我所做的:
受此答案 https://stackoverflow.com/a/4219386/2009617 的启发,我制作了文件的副本lib/Varien/Data/Collection/Db.php
,将其放在app/core/local/Varien/Data/Collection/Db.php
下,并复制了该答案中建议的修改内容,以解决导致上述问题的组选择计数错误.到目前为止,它似乎仍然有效.
Alright, solved it. This is what I did:
Inspired by this answer https://stackoverflow.com/a/4219386/2009617, I made a copy of the file lib/Varien/Data/Collection/Db.php
, placed it under app/core/local/Varien/Data/Collection/Db.php
and copied the modifications suggested on that answer to fix the group select count error that was giving me problems above. So far it seemed to work.
但是,行中有一个问题,当我单击订单时,它说订单不再存在",因此我检查了实际网址,发现网址中的order_id与"entity_id"相同在order_address表中,与订单的实际关联ID(parent_id)不对应.在对MySQL查询进行了长时间的调整之后,我意识到问题出在我制作的/app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
中的_prepareColumns()
和getRowUrl()
函数调用的函数中,因为它们检索了错误的ID.因此,我进行了以下更改:
However, there was a problem in the rows, when I clicked on the orders it said the Order "no longer exists", so I checked the actual url and turns out the order_id in the url was the same as the "entity_id" in the order_address table, which didn't correspond with the actual associative id of the order (parent_id). After tweaking for a long time with the MySQL query, I realized the issue was in the functions called by the _prepareColumns()
and getRowUrl()
functions in the /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
I made, since they were retrieving the wrong Id. So I made the following changes:
在_prepareColumns()
中,在与操作"列相对应的代码中,我将'getter'更改为'getParentId',如下所示:
In _prepareColumns()
, within the code corresponding to the Action column, I changed the 'getter' to 'getParentId', like this:
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()
函数中更改了$ row语句:
And in the getRowUrl()
function, I changed the $row statement within the getUrl()
function like 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 $this->getUrl('*/sales_order/view', array('order_id' => $row->getParentId()));
}
return false;
}
现在,它就像一种魅力.我希望这对其他人有帮助.
And now it works like a charm. I hope this helps somebody else.
这篇关于在订单网格中添加自定义列(Magento 1.7.0.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!