Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Adding a column to Magento orders grid - alternative way using layout handles

  1. #1
    Junior Member golddev's Avatar
    Join Date
    Mar 2013
    Posts
    41
    Thanks
    1
    Thanked 9 Times in 5 Posts

    Post Adding a column to Magento orders grid - alternative way using layout handles

    First of all, you should create a new module (in our case Atwix_ExtendedGrid) and define layout update for adminhtml:

    HTML Code:
    <!--file: app/code/local/Atwix/ExtendedGrid/etc/config.xml-->
    <adminhtml>
        ...
        <layout>
            <updates>
                <atwix_extendedgrid>
                    <file>atwix/extendedgrid.xml</file>
                </atwix_extendedgrid>
            </updates>
        </layout>
        ...
    </adminhtml>
    Note, this configuration is enough for adding fields from sales_flat_order_grid table (no sql joins are needed). But most likely, you will also need to add some field from the external table. For this purpose we use sales_order_grid_collection_load_before event to join extra tables. Here is how the observer configuration looks like:

    HTML Code:
    <!--file: app/code/local/Atwix/ExtendedGrid/etc/config.xml-->
    <adminhtml>
        ...
        <events>
            <sales_order_grid_collection_load_before>
                <observers>
                    <atwix_extendedgrid>
                        <model>atwix_extendedgrid/observer</model>
                        <method>salesOrderGridCollectionLoadBefore</method>
                    </atwix_extendedgrid>
                </observers>
            </sales_order_grid_collection_load_before>
        </events>
        ...
    </adminhtml>
    Then, add a function that handles our event to the observer:

    PHP Code:
    /*file: app/code/local/Atwix/ExtendedGrid/Model/Observer.php*/
        /**
         * Joins extra tables for adding custom columns to Mage_Adminhtml_Block_Sales_Order_Grid
         * @param $observer
         */
        
    public function salesOrderGridCollectionLoadBefore($observer)
        {
            
    $collection $observer->getOrderGridCollection();
            
    $select $collection->getSelect();
            
    $select->joinLeft(array('payment'=>$collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id',array('payment_method'=>'method'));
        } 
    As you can see, we join sales_flat_order_payment table to get a payment method field.

    Now, it is time to add this field to the grid. We will use layout handles to call addColumnAfter method of sales_order.grid (Mage_Adminhtml_Block_Sales_Order_Grid) block. There are two handles that we are interested in: adminhtml_sales_order_index and adminhtml_sales_order_grid. The same code goes to both sections, so we will define a new handle sales_order_grid_update_handle and use update directive. And finally, here is how our layout file looks like:


    HTML Code:
    <!--file: app/design/adminhtml/default/default/layout/atwix/extendedgrid.xml -->
    <layout>
        <sales_order_grid_update_handle>
            <reference name="sales_order.grid">
                <action method="addColumnAfter">
                    <columnId>payment_method</columnId>
                    <arguments>
                        <header>Payment Method</header>
                        <index>payment_method</index>
                        <filter_index>payment.method</filter_index>
                        <type>text</type>
                    </arguments>
                    <after>shipping_name</after>
                </action>
            </reference>
        </sales_order_grid_update_handle>
        <adminhtml_sales_order_grid>
            <!-- apply layout handle defined above -->
            <update handle="sales_order_grid_update_handle" />
        </adminhtml_sales_order_grid>
        <adminhtml_sales_order_index>
            <!-- apply layout handle defined above -->
            <update handle="sales_order_grid_update_handle" />
        </adminhtml_sales_order_index>
    </layout>
    After all steps you should see your column:



    Here is a source code of the completed module. I hope you will find this article useful! Thanks for reading us!

    View more threads in the same category:


  2. #2
    Junior Member Amasty's Avatar
    Join Date
    May 2013
    Posts
    396
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default

    Great tutorial!
    This can also be achieved with a special extension - https://amasty.com/extended-order-grid.html
    Name:  magento-extended-order-grid-additional-columns.png
Views: 1051
Size:  117.2 KB

Similar Threads

  1. Adding new category attribute in Magento
    By golddev in forum Programming & Development
    Replies: 22
    Last Post: 24-01-2024, 10:52 PM
  2. How to Manage Orders in Magento
    By linh in forum Webmaster & Administrator
    Replies: 10
    Last Post: 25-09-2023, 09:53 AM
  3. Adding Cache Support to Magento Blocks
    By sgdev in forum Magento Speed up, Performance and Optimize
    Replies: 2
    Last Post: 29-05-2014, 10:47 AM
  4. Adding currency to woocommerce?
    By speed2x in forum Wordpress
    Replies: 2
    Last Post: 31-07-2013, 12:47 PM
  5. magento order grid add tax/vat number customer
    By jacadesing in forum Programming Questions
    Replies: 1
    Last Post: 06-07-2013, 01:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •