php - 订单网格的公司名称列

标签 php magento grid block

我正在开发包含主题的 Magento 1.8.1。还涉及多个扩展,并且大多数是完全定制的。

一段时间以来,我一直在尝试让公司名称出现在销售订单网格中,并且已经获得了一些运气。

首先,我已将 /core/Mage/Adminhtml/Block/Sales/Order/Grid.php 复制到 /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();
}

同时将以下内容添加到 _prepareColumns:

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

一切都按原样运行,直到我决定将 Ship to Company 也提供给公司会非常好,因为我们的大多数客户都为其他公司购买。

为了做到这一点,我像以前一样毫无问题地添加了列:

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

添加的列没有问题,只是它不在我放置的位置(在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'));

但是当我尝试这个时,我得到一个错误:

You cannot define a correlation name 'addressTable' more than once

我在两列之间也有相当多的冲突。例如,当我没有在 _prepareColumns 下评论 Ship to Company 时,它会显示 Ship to Company 列,Bill to Company 列应该在哪里。列中也没有数据。只要我评论运送到公司的账单到公司就会出现并具有正确的数据。

基本上,我只需要显示 Ship to Company 列以及 Bill to Company 列即可。最好在适当的名称旁边。

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

最佳答案

好的,所以我上面的编辑并不完全正确。这些列显示并填充得很好,但是在尝试搜索该字段后我收到了一个错误。所以经过更多研究,我找到了这个解决方案:Using column aliases in Sales Order Grid field

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

    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();
}

在 _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'
    );

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

有了这个修复,一切终于按我想要的方式工作了!

编码愉快!

关于php - 订单网格的公司名称列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24516967/

相关文章:

php - 如何使 PHP 中的用户 session 过期?

css - Foundation 仅对中等屏幕使用偏移量

php - 在 Magento 中隐藏或删除捆绑产品的数量框

hadoop - PIG - 将字符串连接到参数?

html - Excel 样式 2d 可滚动网格,在 CSS 中带有水平和垂直标签

php - Mongodb php获取新文档的ID?

php - 我应该如何在 php 中使用 dom 获取这样的 div 内容?

PHP 查找最接近时间线周期的日期

magento - 通过布局 XML 文件向 Helper 类传递参数

mysqldump 恢复错误 - 字段类型时间戳的默认值无效