0

I have following code in Observer.php, which basically adds "company name" from billing address to sales order grid.

I would like to add also "name" (Account Name/Surname) and "shipping_company" (Company Name from shipping address).

Columns appears just fine, but I get no data/values.

I guess I need to add a couple of join for the database but I have no idea on how to do this.

Any help is appreciated. Here is my code:

public function addColumnsToSalesOrderGrid(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', // Company Name from billing address
            'type' => 'text',
        );

        $this->_grid->addColumnAfter('company', $columnData, 'created_at');
        $this->_grid->sortColumnsByOrder();



        $columnData = array(
            'header' => 'Name',
            'index'  => 'name', // Account Name/Surname
            'type' => 'text'
        );

        $this->_grid->addColumnAfter('name', $columnData, 'company');
        $this->_grid->sortColumnsByOrder();




        $columnData = array(
            'header' => 'Ship to',
            'index'  => 'shipping_company', // Company Name from shipping address
            'type' => 'text'
        );

        $this->_grid->addColumnAfter('shipping_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();
    }
}
2

try below code

public function addColumnsToSalesOrderGrid(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();

        $select = $this->_collection->getSelect();
        $resource = Mage::getSingleton('core/resource');
        $select->join(
            array('order' => $resource->getTableName('sales/order')),
            'main_table.entity_id = order.entity_id',
            array('name' => 'customer_firstname')
        );

        $select->join(
            array('billing' => $resource->getTableName('sales/order_address')),
            'order.billing_address_id = billing.entity_id',
            array('billing_company' => 'company')
        );

        $select->joinLeft(
            array('shipping' => $resource->getTableName('sales/order_address')),
            'order.shipping_address_id = shipping.entity_id',
            array('shipping_company' => 'company')
        );


        $columnData = array(
            'header' => 'Company Name',
            'index'  => 'billing_company', // Company Name from billing address
            'type' => 'text',
        );

        $this->_grid->addColumnAfter('billing_company', $columnData, 'created_at');
        $this->_grid->sortColumnsByOrder();



        $columnData = array(
            'header' => 'Name',
            'index'  => 'name', // Account Name/Surname
            'type' => 'text'
        );

        $this->_grid->addColumnAfter('name', $columnData, 'billing_company');
        $this->_grid->sortColumnsByOrder();




        $columnData = array(
            'header' => 'Ship to',
            'index'  => 'shipping_company', // Company Name from shipping address
            'type' => 'text'
        );

        $this->_grid->addColumnAfter('shipping_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();
    }
}

EDIT : (Not Tested)

To add two field use Renderer

$columnData = array(
                'header' => 'Name',
                'index'  => 'name', // Account Name/Surname
                'type' => 'text'
                'renderer' => new Company_Module_Block_Adminhtml_Renderer_Getname() 
            );

in your module

<?php 
class Company_Module_Block_Adminhtml_Renderer_Getname extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
         return $row->getFirstname().' '.$row->getLastname();
    }
}
  • Hi Gopal, thank you for your help. It seems to work properly. Is it possible to display customer full name (first name + last name) instead of customer_firstname ? – Antony Apr 12 '17 at 8:43
  • @Antony try updated answer and let me know the status – Gopal Patel Apr 12 '17 at 9:10
  • it is not clear to me how to integrate the "Company_Module_Block_Adminhtml_Renderer_Getname" part with existing code, should I create a new module? – Antony Apr 12 '17 at 10:04
  • Upps wrong code pasted before, you can use existing module just create block renderer file in it. @Antony – Gopal Patel Apr 12 '17 at 10:16
  • I added to Observer : public function render(Varien_Object $row) { return $row->getFirstname().' '.$row->getLastname(); } just before: public function addColumnsToSalesOrderGrid(Varien_Event_Observer $observer) Is that the right place? – Antony Apr 12 '17 at 12:34
0

You can add all these fields and more using the BL CustomGrid extension. It's free and available on GitHub here: https://github.com/mage-eag/mage-enhanced-admin-grids

Although it says it's un-maintained, we have been using it with great success in many stores for years now. It works great in v1.9.3.2.

  • Thank you for your advice, but I'd prefer not to use an extension. – Antony Apr 13 '17 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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