3

In the Order Grid, I want to add the telephone number which customers used in order -> shipping or billing. I can add the title in the grid but I don't know how get the phone number.

The event that I am using for this is sales_order_grid_collection_load_before however I can't get order id using this.

Here is my code.

config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Elavarasan_OrderGrid>
      <version>0.1.0</version>
    </Elavarasan_OrderGrid>
  </modules>
  <global>
    <helpers>
      <ordergrid>
        <class>Elavarasan_OrderGrid_Helper</class>
      </ordergrid>
    </helpers>
    <models>
      <ordergrid>
        <class>Elavarasan_OrderGrid_Model</class>
        <resourceModel>ordergrid_mysql4</resourceModel>
      </ordergrid>
    </models>
    <events>
      <core_block_abstract_to_html_before> <!-- identifier of the event we want to catch -->
        <observers>
          <core_block_abstract_to_html_before_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>ordergrid/observer</class> <!-- observers class alias -->
            <method>getGrid</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </core_block_abstract_to_html_before_handler>
        </observers>
      </core_block_abstract_to_html_before>

      <sales_order_grid_collection_load_before> <!-- identifier of the event we want to catch -->
        <observers>
          <sales_order_grid_collection_load_before_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>ordergrid/observer</class> <!-- observers class alias -->
            <method>addPhoneNo</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </sales_order_grid_collection_load_before_handler>
        </observers>
      </sales_order_grid_collection_load_before>
    </events>
  </global>
</config> 

Observer.php

<?php
class Elavarasan_OrderGrid_Model_Observer
{

            public function getGrid(Varien_Event_Observer $observer)
            {
                $block = $observer->getEvent()->getBlock();
                if ($block->getId() == 'sales_order_grid') {
                    $block->addColumnAfter(
                            'telephone',
                            array(
                                    'header'   => Mage::helper('sales')->__('Telephone No'),
                                    'align'    => 'left',
                                    'type'     => 'options',
                                    'options'  => $paymentMethods,
                                    'index'    => 'telephone',
                                    'filter_index' => 'telephone.telephone'
                            ),
                            'shipping_name'
                    );

                    //$block->sortColumnsByOrder();

                }

            }

            public function addPhoneNo(Varien_Event_Observer $observer)
            {
              echo $phone = $observer->getOrder()->getCustomer()->getPrimaryBillingAddress()->getTelephone();

            }

}
2

A stock sales order grid for Magento 1.9 will prepare its collection in Mage_Adminhtml_Block_Sales_Order_Grid::_prepareCollection, and it won't do much out of the box. In fact the base query for the collection is very simple:

SELECT `main_table`.* FROM `sales_flat_order_grid` AS `main_table`

So in your 2nd observer method you will have to add order addresses to the collection before you can render them in the grid:

public function addPhoneNo(Varien_Event_Observer $observer)
{
    ...
    $observer->getCollection()->addAddressFields();
    // Use below if you want to inspect the query
    // Zend_Debug::dump($observer->getSelect()->assemble());
    ...
}

Now you're able to build your column in the first observer:

public function getGrid(Varien_Event_Observer $observer)
{
    ...
    $block = $observer->getEvent()->getBlock();

    if ($block->getId() == 'sales_order_grid') {
        $block->addColumnAfter(
            'telephone',
            array(
                'header'        => Mage::helper('sales')->__('Telephone No'),
                'align'         => 'left',
                'type'          => 'text',
                'index'         => 'telephone',
                'filter_index'  => 'shipping_o_a.telephone',
            ),
            'shipping_name'
        );
    }
    ...
}

Notice the filter_index used. You're actually pulling the shipping address phone number as written to the sales_order_flat_address table. You can optionally use billing_o_a.telephone if desired (use the Zend_Debug::dump statement in my example of the columns available on the query result.

0

For add telephone number in sales order grid

$collection = Mage::getResourceModel($this->_getCollectionClass());
$prefix = Mage::getConfig()->getTablePrefix();
$collection->getSelect()
    ->joinleft(
        array("sales_flat_order_address" => $prefix."sales_flat_order_address"),
        "sales_flat_order_address.parent_id = main_table.entity_id",
        array("telephone" => "telephone")
    )->distinct('main_table.entity_id');
$collection->addFilterToMap("telephone","`sales_flat_order_address`.`telephone`"); // help for filter
  • But when I add this column in _prepareColumns then I am getting blank why ? $this->addColumn('telephone', array( 'header' => Mage::helper('sales')->__('telephone'), 'width' => '80px', 'type' => 'text', 'index' => 'telephone', )); – Amaresh Tiwari Aug 18 '17 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.