3

I need the tracking number on the Shipments grid in the Back-End,

from here: enter image description here

to here: enter image description here

How can I do this?

2 Answers 2

4

Please try below code :

Overwrite class : Mage_Adminhtml_Block_Sales_Shipment_Grid

Change Collection Method as Below :

 protected function _prepareCollection(){
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $shipTable = Mage::getSingleton('core/resource')->getTableName('sales/shipment_track');
    $collection->getSelect()->joinLeft($shipTable, $shipTable.'.order_id = main_table.order_id',array('track_number'=> $shipTable.'.track_number'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
   }

After This Create Column for Tracking Number :

$this->addColumn('track_number', array(
    'header' => Mage::helper('sales')->__('Tracking Number'),
    'index' => 'track_number',
    'filter_index'=>'sales_flat_shipment_track.track_number',
));

Work For me Happy Coding. :)

29
  • Well come :) Happy Coding.
    – Ajay Patel
    Apr 11, 2017 at 7:18
  • 1
    I would suggest not to use full table name as it will not work if there is prefix added in database tables. You can use Mage::getSingleton('core/resource')->getTableName('sales/shipment_track') to get table name.
    – John
    Apr 11, 2017 at 11:36
  • 1
    @Rathinam You can search tracking number from at top box
    – Ajay Patel
    Apr 11, 2017 at 11:39
  • 1
    If you have used prefix for your table names, then this code will not work. But you don't then its fine
    – John
    Apr 11, 2017 at 11:45
  • 1
    @John Please check my updated ans
    – Ajay Patel
    Apr 11, 2017 at 11:47
3

A better approach to this will be an observer.
Add observer code in your config.xml file

<adminhtml>
    <events>
      <adminhtml_block_html_before>
            <observers>
                <addShipmentNumberToShipmentGrid>
                    <class>Vendor_Module_Model_Observer</class>
                    <method>addShipmentNumberToShipmentGrid</method>
                </addShipmentNumberToShipmentGrid>
            </observers>
        </adminhtml_block_html_before>
    </events>
</adminhtml>

Now create Observer.php with below code

<?php
class Vendor_Module_Model_Observer
{
    public function addShipmentNumberToShipmentGrid(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $this->_grid = $event->getBlock();
        if ($this->_grid instanceof Mage_Adminhtml_Block_Sales_Shipment_Grid) {
            $this->_collection = $this->_grid->getCollection();
            $this->_collection->getSelect()->joinLeft(Mage::getSingleton('core/resource')->getTableName('sales/shipment_track'), "main_table.order_id = ".Mage::getSingleton('core/resource')->getTableName('sales/shipment_track').".order_id",array('track_number'));

            $columnData = array(
                'header' => 'Tracking Number',
                'index'  => 'track_number',
                'type' => 'text',
            );

            $this->_grid->addColumnAfter('track_number', $columnData, 'shipping_name');
            $this->_grid->sortColumnsByOrder();
            $filter = $this->_grid->getParam($this->_grid->getVarNameFilter(), null);
            if (is_null($filter)) {
                $this->_collection->load();
            }
            $this->_collection->clear();
            if (is_string($filter)) {
                $data = $this->_grid->helper('adminhtml')->prepareFilterString($filter);
                $this->_setFilterValues($data);
            } else {
                if ($filter && is_array($filter)) {
                    $this->_setFilterValues($filter);
                }
            }
            $this->_collection->load();
        }
    }

    protected function _setFilterValues($data)
    {
        foreach ($this->_grid->getColumns() as $columnId => $column) {
            if (isset($data[$columnId]) && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0)
                && $column->getFilter()
            ) {
                $column->getFilter()->setValue($data[$columnId]);
                $this->_addColumnFilterToCollection($column);
            }
        }

        return $this;
    }

    protected function _addColumnFilterToCollection($column)
    {
        if ($this->_collection) {
            $field = ($column->getFilterIndex()) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            } else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    $this->_collection->addFieldToFilter($field, $cond);
                }
            }
        }

        return $this;
    }
}

It will also work for filter values.

3
  • @ Jaimin Sutariya : above code good stuff,
    – user51361
    Apr 11, 2017 at 12:08
  • @ Jaimin Sutariya : did you know about Payeezy payment gateway configuration,,
    – user51361
    Apr 11, 2017 at 12:09
  • Thank, but I already told you I am not aware of Payeezy Apr 11, 2017 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.