Adding to Grid Selection for grid.php file

随声附和 提交于 2019-12-11 02:49:21

问题


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:


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.



来源:https://stackoverflow.com/questions/6684684/adding-to-grid-selection-for-grid-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!