Geo*_*off 3 grid magento magento-1.5
通过此问答,我找到了将客户电子邮件地址放在Magento管理销售订单网格上的好方法(http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to-order-grid- in-magento-1-4/6906254#6906254)Ben Incani,它的效果很好.
我的问题是:使用此方法,如何将Ship添加到信息(名称,地址,城市,州,邮政编码)?
我试过做两个版本(那种工作)但是没有完全工作所以我有点卡住了......
这是适用于客户电子邮件的代码:
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email', 'sfo.shipping_description'));
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试进入具有Ship to信息的数据库表时,我尝试了这个:
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.parent_id=sfo.entity_id',array('sfoa.postcode'));
Run Code Online (Sandbox Code Playgroud)
这将返回错误日志,其中包含以下消息:
a:5:{i:0; s:68:"Item(Mage_Sales_Model_Order)具有相同的id"10860"已经存在"; i:1; s:5104:"#0
尝试此代码(最接近原始客户电子邮件代码):
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.entity_id=main_table.entity_id',array('sfoa.postcode'));
Run Code Online (Sandbox Code Playgroud)
产生一个我可以用填充列查看的网格.但是,列中的值不是正确的邮政编码 - 我甚至无法弄清楚它拉的是什么值?
我想我的一个问题是我不知道main_table.entity_id指的是什么(虽然我有猜测).
无论如何,我觉得我很亲密,如果有人能回答我用这种方法成功获得信息的方式,我将永远感激不尽!任何人都可以
Geo*_*off 10
修改后的答案(由于两个有问题的错误)
我正以一种更友好,一步一步的方式重写这个答案,希望能帮助别人.
注意:这是为了进行本地更改app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
答:为了使一切工作正常,您首先需要更改_getCollectionClass()以下内容:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
Run Code Online (Sandbox Code Playgroud)
对此:
protected function _getCollectionClass()
{
//return 'sales/order_grid_collection';
return 'sales/order_collection';
}
Run Code Online (Sandbox Code Playgroud)
这样做时我遇到了一个令人头痛的问题:
SQLSTATE [23000]:完整性约束违规:1052 where子句中的'created_at'列不明确
当您尝试按" 购买时"列筛选/搜索网格时,会发生这种情况.
为了避免/修复这个错误,你需要更改collectioin 及以下内容添加到_prepareCollection() 与添加filter_index到每个加入到网格列.
你也会遇到另一个头痛的问题
SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'billing_name'
如果在_prepareCollection()您尝试动态创建结算名称或运输名称的列,如下所示:
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s2.firstname, ' ',s2.lastname) AS billing_name"));
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s1.firstname, ' ',s1.lastname) AS shipping_name"));
Run Code Online (Sandbox Code Playgroud)
完成后,没有真正简单的方法(我遇到了解决这个问题),如果这一切.
为了避免这些麻烦(在您更改_getCollectionClass()为上述之后),请执行以下操作:
B.更改_prepareCollection()此:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total'));
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
Run Code Online (Sandbox Code Playgroud)
C.然后为现有列_prepareColumns()添加filer_index每个:
例:
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name',
));
Run Code Online (Sandbox Code Playgroud)
D.然后添加要添加的列,如下所示:
例:
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Customer Email'),
'index' => 'customer_email',
'filter_index' => 'sfo.customer_email',
'width' => '50px',
));
Run Code Online (Sandbox Code Playgroud)