Magento - 如何在Magento 1.5.1.0中将Ship发送到信息以订购网格

时间:2012-01-26 00:39:12

标签: grid magento-1.5 magento

通过此Q& A(http://stackoverflow.com/questions/6416864/how-to-add-customer-email-to,我找到了将客户电子邮件地址放在Magento管理销售订单网格上的好方法-order-grid-in-magento-1-4 / 6906254#6906254) Ben Incani ,效果很好。

我的问题是:使用此方法,如何添加发送至信息(名称地址城市< / strong>,压缩)?

我已经尝试过两个版本(那种工作)但是没有完全工作所以我有点卡住...

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

$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:“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'));

产生一个我可以用填充列查看的网格。但是,列中的值是 NOT 正确的邮政编码 - 我甚至无法弄清楚它正在拉什么值???

我想我的一个问题是我并不完全知道 main_table.entity_id 指的是什么(虽然我有猜测)。

无论如何,我觉得我很亲密,如果有人能回答我用这种方法成功获取信息的方式,我将永远感激不尽!任何人都可以

3 个答案:

答案 0 :(得分:10)

修订后的答案(由于两个有问题的错误)

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

注意:这是针对app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

的本地更改

A。为了使所有内容正常运行,您首先需要更改_getCollectionClass()

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

到此:

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

我这样做时遇到了一个令人头痛的问题:

  

SQLSTATE [23000]:完整性约束违规:1052 where子句中的'created_at'列不明确

当您尝试通过购买列过滤/搜索网格时会发生这种情况。

为了避免/修复此错误,您需要更改收集 AND 将以下内容添加到_prepareCollection() AND 添加filter_index添加到网格中的每个列。

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

  

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

您尝试动态创建结算名称发货名称的列的{{1> IF ,如下所示:

_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")); 更改为上述内容),请执行以下操作:

B。_getCollectionClass()更改为:

_prepareCollection()

C。然后,对于 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(); } 中的现有列,为每个列添加_prepareColumns()

示例:

filer_index

D。然后添加您要添加​​的列,如下所示:

示例:

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

答案 1 :(得分:0)

我有一段时间有类似的要求,我需要在销售订单网格中添加客户电子邮件和送货区域。
为了达到要求,我在自定义模块中重写了类Mage_Adminhtml_Block_Sales_Order_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();
    }

}

我希望这会对别人有所帮助。

答案 2 :(得分:0)

该错误的原因&#34; Mage_Sales_Model_Order)具有相同的ID&#34; 10860&#34;已经存在&#34;它是因为sales_flat_order_address为每个订单创建了两个记录,一个用于发货地址,另一个用于记帐地址,如果您在_prepareCollection()上添加此过滤器,则可以使其正常工作

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