php - 添加到 grid.php 文件的网格选择

标签 php variables magento collections mage

我问了类似的问题,但我没有提供足够的详细信息,也没有得到答案,所以我会再试一次。

主要任务是将字段添加到在magento admin sales->invoices下导出的CSV文件中。我找到了要编辑的主文件:

app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Grid.php

它具有添加列的选项,如下所示:

    $this->addColumn('increment_id', array(
        'header'    => Mage::helper('sales')->__('Invoice #'),
        'index'     => 'increment_id',
        'type'      => 'text',
    ));

现在,当我尝试添加新列时,我将索引更改为适当的数据库字段,例如“税额”。唯一的问题是这个新值不在我的 Magento 集合中,因此它只是填充表中的一个空列。

我对 Magento 还很陌生,所以我不完全理解 Magento 集合是如何工作的,或者我如何在 grid.php 的范围内访问它。有人可以指导我如何添加到集合中吗?

我真的很困惑,非常感谢您的帮助。

最佳答案

您基本上需要编辑资源模型以包含您想要包含的字段。您可以在代码中编辑资源,我不确定您使用的版本,但在 Grid.php 文件中您会看到 _prepareCollection 找到看起来像这样的代码,

$collection = Mage::getResourceModel('sales/order_invoice_collection')
->addAttributeToSelect('order_id')
->addAttributeToSelect('increment_id')
->addAttributeToSelect('created_at')
->addAttributeToSelect('state')
->addAttributeToSelect('grand_total') ...and so on!

添加行

->addAttributeToSelect('tax_amount')

到该列表以及您应该能够使用的

$this->addColumn('tax_amount', array(
    'header'    => Mage::helper('sales')->__('Tax'),
    'index'     => 'tax_amount',
    'type'      => 'number',
));

这是未经测试的,因为我远离我的开发机器并且没有法师在手,但这应该有效,或者至少为您指明了正确的方向。

编辑:

如果失败,您可以尝试替换整个 _prepareCollection

protected function _prepareCollection()
{

$collection = Mage::getResourceModel('sales/order_invoice_collection')
->addAttributeToSelect('order_id')
->addAttributeToSelect('increment_id')
->addAttributeToSelect('created_at')
->addAttributeToSelect('state')
->addAttributeToSelect('grand_total')
->addAttributeToSelect('tax_amount')
->addAttributeToSelect('order_currency_code')
->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
->joinAttribute('order_increment_id', 'order/increment_id', 'order_id', null, 'left')
->joinAttribute('order_created_at', 'order/created_at', 'order_id', null, 'left');

$this->setCollection($collection);
return parent::_prepareCollection();
}

这又是未经测试的,根据内存,这是来自 magento 1.3 系列的 _prepareCollection,所以它有点旧,但很确定它应该可以工作。

关于php - 添加到 grid.php 文件的网格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6684684/

相关文章:

php - 如何从函数返回对象?

php - 获取数组第一项的最快方法是什么?

Python:变量、继承和默认参数

javascript - 将从 javascript 函数返回的值分配给变量

php - 在 cdata php 中使用 Strong - Magento

php - Magento ORM 文档

javascript - 如何打印当前用户的地址

javascript - jquery DOM对象内部的自定义变量属性

javascript - 电话号码验证正则表达式

magento - 使用 Magento 企业版结账时未显示的奖励积分付款