2

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49

1 Answers1

4

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.

Alan Cole
  • 1,695
  • 12
  • 13
  • I tried the addAttributeToSelect and it must throw an error because it crashes my dev site. My $collection variable is a bit different from yours. Mine looks like `$collection = Mage::getResourceModel($this->_getCollectionClass());`. Ideas? – Nathaniel Wendt Jul 13 '11 at 20:44
  • What version of Magento are you using? you could try replacing your whole _prepareCollection with another one, see answer above. – Alan Cole Jul 13 '11 at 20:59
  • I'm using Magento 1.9. Yep replacing it did the trick. I would love to understand where it was getting the collection from before, but for now this will work. Thanks! – Nathaniel Wendt Jul 13 '11 at 21:01
  • oh enterprise...this code might not work then, I have an EE version on my machine at home. If that code dosen't work try... $collection = Mage::getResourceModel('sales/order_invoice'); failing that if you've had no luck and no answer I'll post more code when I can get on my DEV box. – Alan Cole Jul 13 '11 at 21:04
  • Also..if you have any Magento resources that you would recommend I would appreciate it. I know they have the resources on their own site which are somewhat limited. Also any books that I try to find seem outdated at best. – Nathaniel Wendt Jul 13 '11 at 21:07
  • There are no real resources to speak of, trail and error really. But feel free to PM any more questions we can pool knowledge :-) – Alan Cole Jul 13 '11 at 21:10
  • when I get the experience I will definitely do that! – Nathaniel Wendt Jul 14 '11 at 03:54
  • @AlanCole let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1437/discussion-between-thepristinedesign-and-alan-cole) – Nathaniel Wendt Jul 14 '11 at 03:55