Magento Invoice Grid new column issue

Magento Invoice Grid new column issue

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Invoice Grid new column issue

Resolving Magento Invoice Grid Column Issue: Adding Customer Company Attribute

As an experienced developer specializing in e-commerce platforms like Magento, I often encounter issues related to customizing grid views and adding new columns. In this post, we’ll dive into a common problem faced by many developers - adding the ‘company’ attribute of a customer to the invoice grid.

The Scenario

A developer wants to display the ‘company’ field from the customer’s address in the Magento invoice grid. They have successfully modified the protected function _prepareCollection() method and queried the database using PHPMyAdmin, which shows correct results. However, when viewing the invoice grid, it only displays a total of 2 records, even though there should be more.

The Current Approach

The developer has attempted to join the sales_flat_order_address table with the main sales_flat_invoice_grid table on the parent_id field. They are using Magento’s resource model and adding the company field from the joined table to their collection. Here is the relevant code snippet:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->getCollectionClass());
    $collection->getSelect()->joinInner(array('order_address' => Mage::getSingleton('core/resource')->getTableName('sales_flat_order_address')),
        'order_address.parent_id = main_table.order_id', array('company'))
        ->group('parent_id')
        ->order('entity_id', 'desc');

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

The query generated by this code is:

SELECT `main_table*.*`, `order_address`.`company` FROM `sales_flat_invoice_grid` AS `main_table` INNER JOIN `sales_flat_order_address` AS `order_address` ON order_address.parent_id = main_table.order_id GROUP BY `parent_id`

The Challenge

Despite the correct query and database results, the developer faces two major issues:

  1. Limited Total Records: Even though there are more invoices in the system, the grid only displays a total of 2 records.

  2. Paging Issue: When increasing the number of records per page to 200, all records are shown correctly. However, the pagination remains broken, and it’s impossible to navigate beyond the first page.

The Solution

The problem lies in how Magento handles collections for grid views. To resolve this issue, you need to ensure that the collection is properly set up for pagination.

Here’s a step-by-step solution:

  1. Set the Loaded Collection: After setting the collection in your _prepareCollection() method, make sure to load it before returning the parent method:
protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->getCollectionClass());
    // ... (your join and sorting code)

    $this->setCollection($collection);
    $collection->load();
    return parent::_prepareCollection();
}
  1. Setup Pagination: Magento grids use the Mage_Page_Block_Html_Pager block for pagination. You need to ensure this block is properly set up:
protected function _prepareLayout()
{
    parent::_prepareLayout();

    $pager = $this->getLayout()->createBlock('page/html_pager');
    $pager->setCollection($this->getCollection());

    // Set some pager properties here, e.g., current page and total pages

    $this->setChild('pager', $pager);
    return $this;
}
  1. Set the Pager in the Layout XML: In your catalog.xml layout file, ensure that you have properly configured the pager for your grid:
<block type="core/text_list" name="invoice.grid.pager">
    <action method="setText"><text>%s</text></action>
    <reference name="invoice_grid_columns_after"/>
</block>

Conclusion

Adding a new column to the Magento invoice grid, such as the customer’s company attribute, can be achieved by modifying the collection in your block. However, proper handling of pagination is crucial for a functional and user-friendly interface.

By setting the loaded collection correctly, configuring the pager block in your layout method, and ensuring that your layout XML file is properly set up, you should be able to resolve this issue and display all invoices with the desired ‘company’ column in the grid.

Remember, always test your changes thoroughly after making modifications to ensure they don’t introduce any new issues or break existing functionality.

For more detailed guides on Magento customization and best practices, visit Laravel Company.

Database Admin Orders Customers

Our network