Connect with us

Blog

Enhanced Order Grid for Magento 1 – Magento Tutorial

mm

Published

on

If you want to add custom fields in order grid, use the following process to custom fields from product and customer grid: First create Block, Helper, controllers, etc folder in the following folder ‘app/code/community/Custom/Orders’.

Now we will add create the files step by step: Create the order.php file in the following folder and add the code below:

Folder Path:  app/code/community/Custom/Orders/Block/Adminhtml/Sales

<?php
 
class Custom_Orders_Block_Adminhtml_Sales_Order extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'custom_orders';
        $this->_controller = 'adminhtml_sales_order';
        $this->_headerText = Mage::helper('custom_orders')->__('<img src="'.Mage::getBaseURL('skin').'adminhtml/default/default/images/catalog/icon/fam_folder_table.gif" /> Orders');
		//$this->_addButtonLabel = 'Create New Orders';
		$this->_addButton('order_reorder', array(
              'label'     => Mage::helper('sales')->__('Create New Orders'),
              'onclick'   => "setLocation('".$this->getUrl('/sales_order_create/start')."')"

          )); 
 
        parent::__construct();
        $this->_removeButton('add'); 
    }
}

?>

Now create grid.php file in order folder and here you can add your custom column which you want to show on order grid:

<?php
 
class Custom_Orders_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('custom_order_grid');
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
	
	
	protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }
 
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('sales/order_collection')
            ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
                'city'       => 'city',
                'country_id' => 'country_id'
            ))
            ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
                'customer_group_code' => 'customer_group_code'
            ))
            ->addExpressionFieldToSelect(
                'fullname',
                'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
                array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
            ->addExpressionFieldToSelect(
                'products',
                '(SELECT GROUP_CONCAT(\' \', x.name)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            )
			->addExpressionFieldToSelect(
                'sku',
                '(SELECT GROUP_CONCAT(\' \', x.sku)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            );
 
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
 
    protected function _prepareColumns()
    {
        $helper = Mage::helper('custom_orders');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
 
        $this->addColumn('increment_id', array(
            'header' => $helper->__('Order #'),
            'index'  => 'increment_id'
        ));
 
        $this->addColumn('purchased_on', array(
            'header' => $helper->__('Purchased On'),
            'type'   => 'datetime',
            'index'  => 'created_at'
        ));
		
		$this->addColumn('fullname', array(
            'header'       => $helper->__('Bill To Name'),
            'index'        => 'fullname',
            'filter_index' => 'CONCAT(customer_firstname, \' \', customer_lastname)'
        ));
		
		
        $this->addColumn('products', array(
            'header'       => $helper->__('Products Name'),
            'index'        => 'products',
            'filter_index' => '(SELECT GROUP_CONCAT(\' \', x.name) FROM sales_flat_order_item x WHERE main_table.entity_id = x.order_id AND x.product_type != \'configurable\')'
        ));
		
			$this->addColumn('sku', array(
            'header'       => $helper->__('Product SKU'),
            'index'        => 'sku',
            'filter_index' => 'sku'
        ));
        
 /*
        $this->addColumn('city', array(
            'header' => $helper->__('City'),
            'index'  => 'city'
        ));
 
        $this->addColumn('country', array(
            'header'   => $helper->__('Country'),
            'index'    => 'country_id',
            'renderer' => 'adminhtml/widget_grid_column_renderer_country'
        ));
 */
  $this->addColumn('customer_email', array(
        'header' => $this->helper('sales')->__('Customer Email'),
        'index' => 'customer_email',
    ));
	
        $this->addColumn('customer_group', array(
            'header' => $helper->__('Customer Group'),
            'index'  => 'customer_group_code'
        ));
 
        $this->addColumn('grand_total', array(
            'header'        => $helper->__('Grand Total'),
            'index'         => 'grand_total',
            'type'          => 'currency',
            'currency_code' => $currency
        ));
 
        $this->addColumn('shipping_method', array(
            'header' => $helper->__('Shipping Method'),
            'index'  => 'shipping_description'
        ));
 
        $this->addColumn('order_status', array(
            'header'  => $helper->__('Status'),
            'index'   => 'status',
            'type'    => 'options',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));
		
		
		 if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
            $this->addColumn('action',
                array(
                    'header'    => Mage::helper('sales')->__('Action'),
                    'width'     => '50px',
                    'type'      => 'action',
                    'getter'     => 'getId',
                    'actions'   => array(
                        array(
                            'caption' => Mage::helper('sales')->__('View'),
                            'url'     => array('base'=>'*/sales_order/view'),
                            'field'   => 'order_id',
                            'data-column' => 'action',
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
            ));
        }
		
 
        $this->addExportType('*/*/exportCustomCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportCustomExcel', $helper->__('Excel XML'));
 
        return parent::_prepareColumns();
    }
	
	
	   protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('order_ids');
        $this->getMassactionBlock()->setUseSelectAll(false);

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/cancel')) {
            $this->getMassactionBlock()->addItem('cancel_order', array(
                 'label'=> Mage::helper('sales')->__('Cancel'),
                 'url'  => $this->getUrl('*/sales_order/massCancel'),
            ));
        }

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/hold')) {
            $this->getMassactionBlock()->addItem('hold_order', array(
                 'label'=> Mage::helper('sales')->__('Hold'),
                 'url'  => $this->getUrl('*/sales_order/massHold'),
            ));
        }

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/unhold')) {
            $this->getMassactionBlock()->addItem('unhold_order', array(
                 'label'=> Mage::helper('sales')->__('Unhold'),
                 'url'  => $this->getUrl('*/sales_order/massUnhold'),
            ));
        }

        $this->getMassactionBlock()->addItem('pdfinvoices_order', array(
             'label'=> Mage::helper('sales')->__('Print Invoices'),
             'url'  => $this->getUrl('*/sales_order/pdfinvoices'),
        ));

        $this->getMassactionBlock()->addItem('pdfshipments_order', array(
             'label'=> Mage::helper('sales')->__('Print Packingslips'),
             'url'  => $this->getUrl('*/sales_order/pdfshipments'),
        ));

        $this->getMassactionBlock()->addItem('pdfcreditmemos_order', array(
             'label'=> Mage::helper('sales')->__('Print Credit Memos'),
             'url'  => $this->getUrl('*/sales_order/pdfcreditmemos'),
        ));

        $this->getMassactionBlock()->addItem('pdfdocs_order', array(
             'label'=> Mage::helper('sales')->__('Print All'),
             'url'  => $this->getUrl('*/sales_order/pdfdocs'),
        ));

        $this->getMassactionBlock()->addItem('print_shipping_label', array(
             'label'=> Mage::helper('sales')->__('Print Shipping Labels'),
             'url'  => $this->getUrl('*/sales_order_shipment/massPrintShippingLabel'),
        ));

        return $this;
    }
	
 
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

?>

Create OrderController.php file in the folder ‘app/code/community/Custom/Orders/controllers’ and add below code:

<?php
 
class Custom_Orders_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));
        $this->loadLayout();
        $this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('custom_orders/adminhtml_sales_order'));
        $this->renderLayout();
    }
 
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid')->toHtml()
        );
    }
 
    public function exportCustomCsvAction()
    {
        $fileName = 'orders_custom.csv';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }
 
    public function exportCustomExcelAction()
    {
        $fileName = 'orders_custom.xml';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}

Now create Adminhtml folder with ordercontroller.php file and add below code:

<?php
 
class Custom_Orders_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));
        $this->loadLayout();
        $this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('custom_orders/adminhtml_sales_order'));
        $this->renderLayout();
    }
	
	public function startAction()
{
    Mage::getSingleton('adminhtml/session_quote')->clear();
    $this->_redirect('*/sales_order_create', array(
        'customer_id' => $this->getRequest()->getParam('customer_id'),
        'store_id'    => $this->getRequest()->getParam('store_id')
    ));
}
 
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid')->toHtml()
        );
    }
 
    public function exportCustomCsvAction()
    {
        $fileName = 'orders_custom.csv';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }
 
    public function exportCustomExcelAction()
    {
        $fileName = 'orders_custom.xml';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}
?>

Create config.xml in etc folder with the following code:

<?xml version="1.0"?>
 
<config>

  <menu>
        <sales>
            <children>
                <sales_orders>
                    <disabled>1</disabled>
                </sales_orders>
            </children>
        </sales>
    </menu>

    <modules>
        <Custom_Orders>
            <version>1.1.1.1</version>
        </Custom_Orders>
    </modules>
 
    <global>
        <models>
            <custom_orders>
                <class>Custom_Orders_Model</class>
                <resourceModel>custom_orders_resource</resourceModel>
            </custom_orders>
        </models>
 
        <resources>
            <custom_orders_setup>
                <setup>
                    <module>Custom_Orders</module>
                </setup>
            </custom_orders_setup>
        </resources>
 
        <blocks>
		  <adminhtml>
            <rewrite>
                <sales_order>Custom_Orders_Block</sales_order>
            </rewrite>
        </adminhtml>

           <!-- <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>Custom_Orders_Block_Adminhtml_Catalog_Category_Tab_Product</catalog_category_tab_product>
					<catalog_product_grid>Custom_Orders_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
				
            </adminhtml>-->
      
		
            <custom_orders>
                <class>Custom_Orders_Block</class>
            </custom_orders>
        </blocks>
 
        <helpers>
            <custom_orders>
                <class>Custom_Orders_Helper</class>
            </custom_orders>
        </helpers>
    </global>
 
   <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <custom_orders before="Mage_Adminhtml">Custom_Orders_Adminhtml</custom_orders>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Now create adminhtml.xml in etc folder:

<?xml version="1.0"?>
 
<config>
    <menu>
        <sales>
            <children>
                <custom_orders translate="title" module="custom_orders">
                    <sort_order>10</sort_order>
                    <title>Orders</title>
                    <action>adminhtml/order/</action>
                </custom_orders>
				<order>
				  <depends>
                                <module>HideMe</module> 
                   </depends>
				</order>
				
            </children>
        </sales>
    </menu>
</config>

Now create blank helper class in helper folder:

<?php
class Custom_Orders_Helper_Data extends Mage_Core_Helper_Abstract
{
 
}

Now create extension enable/disable file in etc/module folder with the name Custom_Orders.xml:

<?xml version="1.0"?>
 
<config>
    <modules>
        <Custom_Orders>
            <active>true</active>
            <codePool>community</codePool>
        </Custom_Orders>
    </modules>
</config>

Now you can see your added column add in order grid.

Also Read :   After Taking World Cup, Harmanpreet Kaur Message To India

 

Hi, Thank you for reading my article. I am Victor, a professional blogger from Jaipur, India.I started webetutorial for blogging & sharing solution for developer questions, and now it’s empowering globally by helping them to make money.I am writing about starting & managing a blogs, WordPress, Magento, Social Media, SEO, Marketing, making money online, Investment, Finance, Gadgets, Fitness and more.

Continue Reading
Advertisement
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

two × four =

Blog

Keys to Blogging Success: How to Write an Awesome Blog Post

mm

Published

on

Keys to Blogging Success: How to Write an Awesome Blog Post

In this article we will talk about how to write a quality blog post. This article will give you some idea to enhance your post quality and increase content word limit.

Follow the below points to write a blog:

Make A Plan:

Before starting on any blog we have to make a plan on which topic we are going to write and how many words we can write. We need to choose the words and headings of content on which readers is driving on search engine. Blog plan will maintain a well-created synopsis with a target goal to reach your readers.

Create A Good Title Of Blog:

You need to stand out in the today competition need a good title for your blog which define your whole blog in a single line. Your title should be completed in a 60 characters because greater title will escape by some search engine like Google.

Make sure your blogs all point is clear from the beginning which creates a connection between you and your readers not only for current blog they will connect for further blogs.

Also Read :   Migrate From Magento 1 To Magento 2 Without Breaking Your Store

Keep It Clean & Formatted:

Blog content should be proper aligned and well-formatted with an organized structure. Only need to describe the information which is important and will not confuse your reader.

First all blogs should write in the Microsoft word or open word which gives formatted structure to your blog and make low chance for wrong spell.  After ensuring that your post is good enough and everything looks fine to be published upload content on your live blogging website.

Write Down The Truth:

When you are going to write a any blog you need to write down the truth content because readers only love true websites. You also need to respect your readers and competitor bloggers to increase traffic on your website.

On-Page SEO:

Blog must have SEO friendly title, meta keywords, meta description, Image alt tags, Headings, Inbound and outbound links. On-page seo will give change to your blog to show on top of search result in search engines. For the WordPress blogs you can install and configuring Yoast SEO plugin.

Also Read :   Panasonic G7 Shoot 4K Video

Wrap Up:

Once you have completed your blog you should write a 2-3 line of short description which you have wrote because that will wrapping your blog overview and make your blog attractive.

For example: In this blog we talk about grows in traffic, revenue, authority topic and sharing personal experiences. The all of things describe here will increase your content quality for blogs.

To get more news on content writing, subscribe us or join us to advertise your blog with us.

Continue Reading

Trending