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 :   Product Attachment for Magento 2

 

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 *

five + fifteen =

Blog

Ways to Increase Comments on Your Blog

mm

Published

on

Ways to Increase Comments on Your Blog

Everyone knows commenting on the other website blogs give the benefits of backlinks, traffic, and branding of our linked website and we are trying lots of things to create a good backlinks which would give a huge converting traffic to us. Sometime people commenting a those website which doesn’t have good reputation or not a much traffic so, their efforts are not useful for them.

Commenting is the ways to drive traffic that is facebook commenting, twitter commenting or website commenting. Here we are going to tell you about some points which may give you huge traffic to increase comments on your blog.

Blog Comment Points Are:

Get visitors to subscribe: Many readers don’t want to comment at first time on your blog because they don’t have much truest on your blog and they don’t know about how much the traffic on your blog getting. To ask them to subscribe to your blog for more blog or webinar this will help to increase comments, traffic and revenue.

Also Read :   How to Use Magento 2 Layouts, Blocks and Templates

Disable spam prevention methods: If you are using captcha, then you need to remove them so user can comment without any interruption.

Avoid forcing registration: If you are using registration process for commenting on your site ten remove them because this create far too much resistance and makes it much more irritate for your visitors to comment.

Link to your comments section at the end of your post: Lot of blog themes have a problem with its comment section, visitors have to scroll the whole page at the bottom for the commenting. Your blog should have comment link at the top of page with blog title where visitor can click and move directly to comment section.

Go back and think about the audience you’re trying to reach: If you are getting many comments then try to give answer of your visitors which give your attention to your visitor and help you to reach your goal and focus on the right areas.

Also Read :   Heena Sidhu shoots gold, Deepak clinches bronze in Commonwealth Shooting Championship

Leave an unanswered question at the end of your post: Ask a question at the end of blog which is the one of the best ways to get readers attention to comment on your blog. Your aim should to develop a discussion within your comments and the more discussion.

Respond to comments and encourage more conversations: As we talk in Go back and think about the audience you’re trying to reach engaging your audience for getting more comments in general ways to respond to comments for further conversation.

You can let us your point of views in the comment or subscribe us to get more blog from us.

Continue Reading

Trending