公司名称订单网格列

RLT*_*ode 2 php grid block magento

所以,这是我在网站上的第一篇文章,但我过去从这个网站获得了相当多的知识,并期待学习更多.我绝对是一个新手,因为我和Magento一起自学成才,所以我很抱歉这篇文章有什么不对.

我正在研究Magento 1.8.1,其中包含一个主题.涉及多个扩展,大多数都是完全自定义的.

问题:我一直试图让公司名称出现在销售订单网格上一段时间,并且运气不错.

首先,我复制了:/ core/Mage/Adminhtml/Block/Sales/Order/Grid.php -to- /local/Mage/Adminhtml/Block/Sales/Order/Grid.php

然后我将_prepareCollection()中的代码更新为:

    protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join(
        array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "billing"',array('billing_company'=>'company'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
Run Code Online (Sandbox Code Playgroud)

将以下内容添加到_prepareColumns:

    $this->addColumn('company', array(
        'header' => Mage::helper('sales')->__('Bill to Company'),
        'index' => 'billing_company',
    ));
Run Code Online (Sandbox Code Playgroud)

好的,到目前为止一切顺利.一切都按照应有的方式运作,直到我决定将船舶运送到公司真的很好,因为我们的大多数客户都在为其他公司购买.

为了实现这一点,我像以前一样添加了没有问题的列:

   $this->addColumn('company', array(
        'header' => Mage::helper('sales')->__('Ship to Company'),
        'index' => 'shipping_company',
    ));
Run Code Online (Sandbox Code Playgroud)

该列添加没有问题,除了它不在我放的地方(在shipping_name之前),这只是添加列没有数据.

现在要添加数据,我会在_prepareCollection下添加另一个集合,因为与表中的结算相比,发货信息在不同的行上?像这样?:

    $collection->getSelect()->join(
        array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "shipping"',array('shipping_company'=>'company'));
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这个时,我得到一个错误: /core/Mage/Adminhtml/Block/Sales/Order/Grid.php

我在两列之间也有很多冲突.例如,当我没有在_prepareColumns下注释Ship to Company时,它会显示Ship to Company列,其中Bill to Company列应该是.列中也没有数据.一旦我评论向公司发送公司账单并且具有正确的数据.

基本上,我只需要显示Ship to Company Column以及Bill to Company Column.最好在相应名称旁边.

任何答案或帮助将不胜感激.

我已经将公司列添加到客户网格并创建新订单客户网格也没有问题.

谢谢,

**编辑**

好吧,我只是想出来,所以我想将我的解决方案发布到我的问题,供其他人查看是否需要:

这是我更新的本地/ Mage/Adminhtml/Block/Sales/Order/Grid.php的代码:

    protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join(
        array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "billing"',array('billing_company'=>'company'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
Run Code Online (Sandbox Code Playgroud)

要将列放在Ship to Name旁边,我必须使用addColumnAfter作为Ship to Company,因为它将它放在最右边.

    $this->addColumn('company', array(
        'header' => Mage::helper('sales')->__('Bill to Company'),
        'index' => 'billing_company',
    ));
Run Code Online (Sandbox Code Playgroud)

瞧,一切都在发挥作用!

RLT*_*ode 8

好的,所以我上面的编辑并不完全正确.列显示并填充正常但在尝试搜索字段后,我收到错误.经过更多的研究,我找到了这个解决方案:在Sales Order Grid字段中使用列别名

这个问题的答案是完美的,只需改变一些小事.我在下面附上我的代码,以防其他人正在寻找这个解决方案.

    protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join(array('address_billing' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_billing.parent_id AND address_billing.address_type = "billing"',array('address_billing.company as billing_company'));
    $collection->getSelect()->join(array('address_shipping' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_shipping.parent_id AND address_shipping.address_type = "shipping"',array('address_shipping.company as shipping_company'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
Run Code Online (Sandbox Code Playgroud)

在_prepareColumns()下更改为:

    $this->addColumn('company', array(
        'header' => Mage::helper('sales')->__('Bill to Company'),
        'index' => 'billing_company',
        'filter_index' => 'address_billing.company',
    ));

    $this->addColumnAfter('shipping_company', array(
        'header' => Mage::helper('sales')->__('Ship to Company'),
        'index' => 'shipping_company',
        'filter_index' => 'address_shipping.company',
        ),
    'billing_name'
    );
Run Code Online (Sandbox Code Playgroud)

请记住,我必须使用addColumnAfter函数将我的运输公司列放在正确的位置.

有了这个解决方案,一切都终于以我想要的方式工作了!

快乐的编码!