为Grid .php文件添加网格选择


Adding to Grid Selection for grid.php file

我问了一个类似的问题,但我没有提供足够的细节,我没有得到答案,所以我再试一次。

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

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

这有选项addColumn的如下:

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

现在,当我尝试添加新的列时,我将索引更改为适当的数据库字段,例如,我们说'tax amount'。唯一的问题是这个新值不在我的Magento集合中,所以它只是在表中填充一个空列。

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

我真的卡住了,如果你能帮助我,我会很感激的。

您基本上需要编辑资源模型以包含您想要包含的字段。你可以在代码中编辑资源,我不知道你用的是哪个版本但在grid。php文件中你会看到_preparecall找到类似于

的代码
$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',
));

这是未经测试的,因为我远离我的开发机器,没有法师的手,但这应该工作或至少指向正确的方向。

编辑:

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

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

这是未经测试的,从记忆中,这是_preparecall从magento的1.3范围,所以它有点旧,但很确定它应该工作