3

I need to show Company Name in sales order grid. These are the files I created:

config.xml

<adminhtml>
    <events>
      <adminhtml_block_html_before>
            <observers>
                <addCompanyNameToSalesOrderGrid>
                    <class>Cpu_CustomGrid_Model_Observer</class>
                    <method>addCompanyNameToSalesOrderGrid</method>
                </addCompanyNameToSalesOrderGrid>
            </observers>
        </adminhtml_block_html_before>
    </events>
</adminhtml>

Observer.php

<?php
class Cpu_CustomGrid_Model_Observer extends Varien_Event_Observer
{
    public function addCompanyNameToSalesOrderGrid(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $this->_grid = $event->getBlock();
        if ($this->_grid instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
            $this->_collection = $this->_grid->getCollection();
            $this->_collection->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left');
            $this->_collection->addAttributeToSelect('company');
            $columnData = array(
                'header' => 'Company Name',
                'index'  => 'company',
                'type' => 'text',
            );

            $this->_grid->addColumnAfter('company', $columnData, 'name');
            $this->_grid->sortColumnsByOrder();
            // rebuild the filters
            $filter = $this->_grid->getParam($this->_grid->getVarNameFilter(), null);
            if (is_null($filter)) {
                $this->_collection->load();
            }
            $this->_collection->clear();
            if (is_string($filter)) {
                $data = $this->_grid->helper('adminhtml')->prepareFilterString($filter);
                $this->_setFilterValues($data);
            } else {
                if ($filter && is_array($filter)) {
                    $this->_setFilterValues($filter);
                }
            }
            // force a reload of the collection
            $this->_collection->load();
        }
    }

    protected function _setFilterValues($data)
    {
        foreach ($this->_grid->getColumns() as $columnId => $column) {
            if (isset($data[$columnId]) && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0)
                && $column->getFilter()
            ) {
                $column->getFilter()->setValue($data[$columnId]);
                $this->_addColumnFilterToCollection($column);
            }
        }

        return $this;
    }

    protected function _addColumnFilterToCollection($column)
    {
        if ($this->_collection) {
            $field = ($column->getFilterIndex()) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            } else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    $this->_collection->addFieldToFilter($field, $cond);
                }
            }
        }

        return $this;
    }
}

Unfortunately I cannot get it to work. What could be the problem?

1 Answer 1

4

Try below code in your observer function.

public function addCompanyNameToSalesOrderGrid(Varien_Event_Observer $observer)
{
    $event = $observer->getEvent();
    $this->_grid = $event->getBlock();
    if ($this->_grid instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
        $this->_collection = $this->_grid->getCollection();
        $this->_collection->getSelect()->join(Mage::getSingleton('core/resource')->getTableName('sales/order_address'), "main_table.entity_id = ".Mage::getSingleton('core/resource')->getTableName('sales/order_address').".parent_id",array('company') )->where(Mage::getSingleton('core/resource')->getTableName('sales/order_address').".address_type =  'billing'");

        $columnData = array(
            'header' => 'Company Name',
            'index'  => 'company',
            'type' => 'text',
        );

        $this->_grid->addColumnAfter('company', $columnData, 'shipping_name');
        $this->_grid->sortColumnsByOrder();
        // rebuild the filters
        $filter = $this->_grid->getParam($this->_grid->getVarNameFilter(), null);
        if (is_null($filter)) {
            $this->_collection->load();
        }
        $this->_collection->clear();
        if (is_string($filter)) {
            $data = $this->_grid->helper('adminhtml')->prepareFilterString($filter);
            $this->_setFilterValues($data);
        } else {
            if ($filter && is_array($filter)) {
                $this->_setFilterValues($filter);
            }
        }
        // force a reload of the collection
        $this->_collection->load();
    }
}
8
  • Hi Jaimin, unfortunately I get a database error, I guess it is due to the fact the my tables have prefix in front.
    – Antony
    Apr 10, 2017 at 9:27
  • Try Mage::getSingleton('core/resource')->getTableName('sales/order_address') Apr 10, 2017 at 9:51
  • Thank you for your efforts Jaimin, but I am now getting: "Column not found: 1054 Unknown column 'sales_flat_order_address.address_type'"
    – Antony
    Apr 10, 2017 at 9:57
  • Yes, use updated code, you need to use Mage::getSingleton('core/resource')->getTableName('sales/ord‌​er_address') for all of your table instances. I have updated the same in answer. Apr 10, 2017 at 9:59
  • Now getting: "Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use". Maybe a typo mistake?
    – Antony
    Apr 10, 2017 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.