Adding to Grid Selection for grid.php file

2019-07-19 02:17发布

I asked a similar question but I did not provide sufficient details and I got no answers so I will try again.

The main task is to add fields to the CSV file that is exported under the magento admin sales->invoices. I found the main file to edit:

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

This has the options to addColumn's like so:

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

Now when I try to add new Column's I change the index to the appropriate database field, lets say 'tax amount' for example. The only problem is that this new value is not in my Magento collection, so it simply populates an empty column in the table.

I am quite new to Magento so I don't fully understand how the Magento collection works or how I can access it for the scope of grid.php. Could someone please give me some direction in how to add to the collection?

I'm really stuck and would appreciate the help.

1条回答
乱世女痞
2楼-- · 2019-07-19 02:38

You basically need to edit the resource model to include the fields you want to include. You can edit the resource in code, I'm not sure what version your using but in the Grid.php file you'll see the _prepareCollection find the code that looks like,

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

add the line

->addAttributeToSelect('tax_amount')

to that list and the you should be able to use

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

This is as untest as I am away from my dev machine and dont have Mage to hand, but this should work or at very least point you in the right direction.

Edit:

Failing that you could try replacing your whole _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();
}

Again this is untested, from memory this is the _prepareCollection from the 1.3 range of magento so its a little old, but quite sure it should work.

查看更多
登录 后发表回答