我问了类似的问题,但我没有提供足够的详细信息,也没有得到答案,所以我会再试一次。
主要任务是将字段添加到在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/