Magento – Join Custom Table with Admin/Sales/Order/View

adminhtmlsales-order

Basically i'm trying to add additional data's to sales -> order -> view page.

I have a custom table named 'additional_order_info' i want to join this table with 'sales_flat_order' using 'order_id'

So far i noted that below function under app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php is responsible to get order information's like Ordered date, etc..

public function getOrder() {
    return Mage::registry('sales_order');
}

So is there way to join my custom table with other sales_order related tables ? So that i can display my custom values under every order view.

Best Answer

Vinoth,You can do this by event and observer. According to magento system,there are two event fire on every collection load. One is collection load before and other is collection load after

Functionality,you can check at on class Mage_Core_Model_Resource_Db_Collection_Abstract

Event on collection load before:

  protected function _beforeLoad()
    {
        parent::_beforeLoad();
        Mage::dispatchEvent('core_collection_abstract_load_before', array('collection' => $this));
        if ($this->_eventPrefix && $this->_eventObject) {
            Mage::dispatchEvent($this->_eventPrefix.'_load_before', array(
                $this->_eventObject => $this
            ));
        }
        return $this;
    }

Event on collection load after:

protected function _afterLoad()
    {
        parent::_afterLoad();
        foreach ($this->_items as $item) {
            $item->setOrigData();
            if ($this->_resetItemsDataChanged) {
                $item->setDataChanges(false);
            }
        }
        Mage::dispatchEvent('core_collection_abstract_load_after', array('collection' => $this));
        if ($this->_eventPrefix && $this->_eventObject) {
            Mage::dispatchEvent($this->_eventPrefix.'_load_after', array(
                $this->_eventObject => $this
            ));
        }
        return $this;
    }

Now if we add the custom table on basic of two event then it can fulfill yours requirement

Basically on the sales order collection load event we can add your custom table.Just Order grid load from sales/order_grid_collection.This collection

collection these events are sales_order_grid_collection_load_before and sales_order_grid_collection_load_after. If we add custom table using joinLeft then it would be working good.

Example:

<adminhtml>
    <events>
        <sales_order_grid_collection_load_before>
            <observers>
                <amit_sales_order_grid_collection_load_before>
                    <class>Yourmodulem modelPrefix/observer</class>
                    <method>addmycustomtable</method>
                </amit_sales_order_grid_collection_load_before>
            </observers>
        </sales_order_grid_collection_load_before>
    </events>
</adminhtml>

Observer code:

<?php class MyNameSpace_Mymodule_Model_Observer
public function addmycustomtable(Varien_Event_Observer $observer)
    {
        $collection = $observer->getOrderGridCollection();
        $select = $collection->getSelect();
        $select->joinLeft(array('v' => 'additional_order_info'),
                        'main_table.entity_id = v.order_id',
                        array('*'));
}
}
Related Topic