Grid Magento-如何在Magento 1.5.1.0中向订单网格添加发货至信息

Grid Magento-如何在Magento 1.5.1.0中向订单网格添加发货至信息,grid,magento-1.5,magento,Grid,Magento 1.5,Magento,我发现了一个很好的方法,通过这个问答将客户的电子邮件地址放到Magento Admin销售订单网格中(http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to-order-grid-in-magento-1-4/6906254#6906254)由本因卡伊创作,效果很好 我的问题是:使用此方法,我如何添加发送至信息(名称,地址,城市,州,邮政编码) 我试过做两个版本(类似的工作),但没有完全工作,所以我有点卡住了

我发现了一个很好的方法,通过这个问答将客户的电子邮件地址放到Magento Admin销售订单网格中(http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to-order-grid-in-magento-1-4/6906254#6906254)由本因卡伊创作,效果很好

我的问题是:使用此方法,我如何添加发送至信息(名称地址城市邮政编码

我试过做两个版本(类似的工作),但没有完全工作,所以我有点卡住了

这是适用于客户电子邮件的代码:

$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email', 'sfo.shipping_description'));
现在,当尝试进入包含收货方信息的数据库表时,我尝试了以下方法:

$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'sfoa.parent_id=sfo.entity_id',array('sfoa.postcode'));
这将返回一个错误日志,其中包含以下消息:

a:5:{i:0;s:68:“已存在具有相同id“10860”的项目(Mage_Sales_Model_Order);i:1;s:5104:”“0”

尝试此代码(最接近原始客户电子邮件代码):

生成一个网格,我可以使用填充的列查看。但是,列中的值不是正确的邮政编码-我甚至无法确定它提取的值是什么

我想我的一个问题是,我不太清楚主表.entity\u id指的是什么(尽管我有一个猜测)

无论如何,我觉得我很接近,如果有人能回答我是如何用这种方法成功获得信息的,我将永远感激!有人能修改答案吗(由于两个问题错误)

我正在以一种更友好、一步一步的方式重写这个答案,希望能帮助其他人

注意:这是针对
app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php的本地更改

A.为了使一切正常工作,首先需要从以下位置更改
\u getCollectionClass()

protected function _getCollectionClass()
{
    return 'sales/order_grid_collection';
}
为此:

protected function _getCollectionClass()
{
    //return 'sales/order_grid_collection';
    return 'sales/order_collection';
}
我在做这件事的时候头疼得厉害,就是这样:

SQLSTATE[23000]:完整性约束冲突:where子句中的1052列“created_at”不明确

当您尝试按购买日期列筛选/搜索网格时,会发生这种情况

为了避免/修复此错误,您需要在中更改集合,并在
\u prepareCollection()
中添加以下内容,并在添加到网格的每个列中添加
过滤器索引

你还会遇到另一个头疼的问题

SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“billing_name”

如果在
\u 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"));
当这一切完成后,如果一切顺利的话,就没有真正简单的方法(我找到了解决这个问题的方法)

为了避免这些麻烦(之后将
\u getCollectionClass()
更改为上述内容),请执行以下操作:

B.
\u 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();
}
C.然后对于
\u prepareColumns()
中的现有列,在每个列中添加一个
filer\u索引

示例:

$this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
        'filter_index' => 'sfog.billing_name',
    ));
$this->addColumn('customer_email', array(
            'header' => Mage::helper('sales')->__('Customer Email'),
            'index' => 'customer_email',
            'filter_index' => 'sfo.customer_email',
            'width' => '50px',
    ));
D.然后添加要添加的列,如下所示:

$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"));
示例:

$this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
        'filter_index' => 'sfog.billing_name',
    ));
$this->addColumn('customer_email', array(
            'header' => Mage::helper('sales')->__('Customer Email'),
            'index' => 'customer_email',
            'filter_index' => 'sfo.customer_email',
            'width' => '50px',
    ));

我以前也有类似的要求,我需要将客户电子邮件和发货区域添加到销售订单网格中。
为了达到要求,我在我的自定义模块中重写了如下类
Mage\u Adminhtml\u Block\u Sales\u Order\u Grid

class Custom_OrderGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid {

    protected function _getCollectionClass() {
        return 'sales/order_collection';
    }

    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.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.region'));

        $this->setCollection($collection);

        return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }

    protected function _prepareColumns() {

        $this->addColumn('real_order_id', array(
            'header' => Mage::helper('sales')->__('Order #'),
            'width' => '80px',
            'type' => 'text',
            'index' => 'increment_id',
            'filter_index' => 'sfo.increment_id'
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('store_id', array(
                'header' => Mage::helper('sales')->__('Purchased From (Store)'),
                'index' => 'store_id',
                'type' => 'store',
                'store_view' => true,
                'display_deleted' => true,
                'filter_index' => 'sfo.store_id'
            ));
        }

        $this->addColumn('created_at', array(
            'header' => Mage::helper('sales')->__('Purchased On'),
            'index' => 'created_at',
            'type' => 'datetime',
            'width' => '100px',
            'filter_index' => 'sfo.created_at'
        ));

        $this->addColumn('billing_name', array(
            'header' => Mage::helper('sales')->__('Bill to Name'),
            'index' => 'billing_name',
            'filter_index' => 'sfog.billing_name'
        ));

        $this->addColumn('shipping_name', array(
            'header' => Mage::helper('sales')->__('Ship to Name'),
            'index' => 'shipping_name',
            'filter_index' => 'sfog.shipping_name'
        ));

        $this->addColumn('customer_email', array(
            'header' => Mage::helper('sales')->__('Customer Email'),
            'index' => 'customer_email',
            'filter_index' => 'sfo.customer_email',
            'width' => '50px',
        ));

        $this->addColumn('region', array(
            'header' => Mage::helper('sales')->__('Shipping State'),
            'index' => 'region',
            'filter_index' => 'sfoa.region',
            'width' => '50px',
        ));

        $this->addColumn('base_grand_total', array(
            'header' => Mage::helper('sales')->__('G.T. (Base)'),
            'index' => 'base_grand_total',
            'type' => 'currency',
            'currency' => 'base_currency_code',
            'filter_index' => 'sfo.base_grand_total'
        ));

        $this->addColumn('grand_total', array(
            'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
            'index' => 'grand_total',
            'type' => 'currency',
            'currency' => 'order_currency_code',
            'filter_index' => 'sfo.grand_total'
        ));

        $this->addColumn('status', array(
            'header' => Mage::helper('sales')->__('Status'),
            'index' => 'status',
            'type' => 'options',
            'width' => '70px',
            'filter_index' => 'sfo.status',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));

        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',
                '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,
            ));
        }
        $this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));

        $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
        $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));

        return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
    }

}

我希望这能帮助其他人。

关于相同id为“10860”的错误“Mage\u Sales\u Model\u Order”的原因已经存在。这是因为Sales\u flat\u Order\u address为每个订单创建了两个记录,一个用于发货地址,另一个用于账单地址,如果在“prepareCollection()上添加此过滤器,则可以使其正常工作

$collection->getSelect()->where("address_type='shipping'");

我能够按照您的上一个代码片段操作邮政编码,并将addColumn调用添加到_prepareColumns。块的小片段:Dan。我猜它不能像那样工作的原因是[对于我们]发货和账单信息[对于父ID]的订单信息重复输入。我实际上已经找到了答案,但由于我的名声不好,我在发布后的8小时内无法回答自己的问题。我将在今晚晚些时候或明天发布我的解决方案。感谢您的输入(以及代码片段)!