আগামী ৩০ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Magento Extension Development Part-2

Create a Customize Orders Module In Magento

In this tutorial, we will create a customize orders module in Magento that can be managed through the admin panel. After finished, you will be able to Browse, filter and sort customized orders in Magento Admin Panel”

We will cover the following topics:

  • Create a customize orders module in Magento.
  • Browse, filter and sort orders in Magento’s admin panel.

As usual, I assume you already read our previous article Magento Extension Development Part-1 and are comfortable with this step, with no explanation required.

Here is our directory structure for the module

  1. /app/etc/modules/Masud_Orders.xml
  2. /app/code/community/Masud/Orders/etc/config.xml
  3. /app/code/community/Masud/Orders/etc/adminhtml.xml
  4. /app/code/community/Masud/Orders/controllers/Adminhtml/OrderController.php
  5. /app/code/community/Masud/Orders/Block/Adminhtml/Sales/Order/Grid.php
  6. /app/code/community/Masud/Orders/Block/Adminhtml/Sales/Order.php
  7. /app/code/community/Masud/Orders/Helper/Data.php
  8. Step 1.Create new module in /app/etc/modules called Masud_Orders.xml

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

    Step 2.Create config.xml inside app/code/community/Masud/Orders/etc/config.xml etc folder.

    <?xml version="1.0"?>
     
    <config>
        <modules>
            <Masud_Orders>
                <version>0.0.0.1</version>
            </Masud_Orders>
        </modules>
     
        <global>
            <models>
                <masud_orders>
                    <class>Masud_Orders_Model</class>
                    <resourceModel>masud_orders_resource</resourceModel>
                </masud_orders>
            </models>
     
     
            <blocks>
                <masud_orders>
                    <class>Masud_Orders_Block</class>
                </masud_orders>
            </blocks>
     
            <helpers>
                <masud_orders>
                    <class>Masud_Orders_Helper</class>
                </masud_orders>
            </helpers>
        </global>
     
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <masud_orders before="Mage_Adminhtml">Masud_Orders_Adminhtml</masud_orders>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
    </config>
    

    Step 3. Create adminhtml.xml file inside app/code/community/Masud/Orders/etc/ folder which will add a link to our orders page in magento admin panel.

    <?xml version="1.0"?>
    <config>
        <menu>
            <sales>
                <children>
                    <masud_orders translate="title" module="masud_orders">
                        <sort_order>10</sort_order>
                        <title>Orders - Masud</title>
                        <action>adminhtml/order/</action>
                    </masud_orders>
                </children>
            </sales>
        </menu>
    </config>
    

    Step 4: Creating Our Helper

    We don’t actually need to define any custom functionality in a helper for this tutorial. But we will be adding menu items to the admin panel that use a helper for translation purposes, so we can simply create one in app/code/community/Masud/Orders/Helper/Data.php and forget about it.

    <?php
     
    class Masud_Orders_Helper_Data extends Mage_Core_Helper_Abstract
    {
     
    }
    

    Step 5: Creating Our Admin Controller

    Now, we need a controller to accept requests and render the container blocks from above. The controller will also handle POST requests to create, update and delete orders as required. create one in app/code/community/Masud/Orders/ controllers/Adminhtml/OrderController.php with this:

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

    Step 7: Next thing we do is create grid container in app/code/community/Masud/Orders/ Block/Adminhtml/Sales/Order.php

    <?php
     
    class Masud_Orders_Block_Adminhtml_Sales_Order extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
            $this->_blockGroup = 'masud_orders';
            $this->_controller = 'adminhtml_sales_order';
            $this->_headerText = Mage::helper('masud_orders')->__('Orders - Masud');
     
            parent::__construct();
            $this->_removeButton('add');
        }
    }
    
    

    Step 8: Last step is making grid class in app/code/community/Masud/Orders/ Block/Adminhtml/Sales/Order/Grid.php

    <?php
     
    <?php
     
    class Masud_Orders_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
    {
        public function __construct()
        {
            parent::__construct();
            $this->setId('masud_order_grid');
            $this->setDefaultSort('increment_id');
            $this->setDefaultDir('DESC');
            $this->setSaveParametersInSession(true);
            $this->setUseAjax(true);
        }
     
        protected function _prepareCollection()
        {
            $collection = Mage::getResourceModel('sales/order_collection')
           
                ->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')
                )
            ;
     
            $this->setCollection($collection);
            parent::_prepareCollection();
            return $this;
        }
     
        protected function _prepareColumns()
        {
            $helper = Mage::helper('masud_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('fullname', array(
                'header'       => $helper->__('Name'),
                'index'        => 'fullname',
                'filter_index' => 'CONCAT(customer_firstname, \' \', customer_lastname)'
            ));
     
    
    
     
            $this->addColumn('grand_total', array(
                'header'        => $helper->__('Grand Total'),
                'index'         => 'grand_total',
                'type'          => 'currency',
                'currency_code' => $currency
            ));
     
     
            $this->addColumn('order_status', array(
                'header'  => $helper->__('Status'),
                'index'   => 'status',
                'type'    => 'options',
                'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
            ));
     
            $this->addExportType('*/*/exportMasudCsv', $helper->__('CSV'));
            $this->addExportType('*/*/exportMasudExcel', $helper->__('Excel XML'));
     
            return parent::_prepareColumns();
        }
     
        public function getGridUrl()
        {
            return $this->getUrl('*/*/grid', array('_current'=>true));
        }
    }
    

    Our new custom orders page can be accessed by going to Sales -> Orders – Masud in admin panel.

    Magento Admin Crud

    Magento Extension Development Part-2

    Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I graduated in 2009 with a bachelor's degree in Engineering from State University Of Bangladesh, I'm also a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, CIDA and MAX Group where I worked on ERP software and web development., but now i'm a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services Ltd. I'm also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute - a leading Training Institute in the country.
One comment on “Magento Extension Development Part-2

Leave a Reply

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