5

What I would like to achieve is to show Account Name + Lastname in sales order grid (like it happens in customer grid).

These are files I created:

Cpu/CustomGrid/etc/config.xml

<config>
    <modules>
        <Cpu_CustomGrid>
            <version>0.4.0</version>
        </Cpu_CustomGrid>
    </modules>
    <global>
        <blocks>
            <getname>
                <class>Cpu_CustomGrid_Block</class>
            </getname>
        </blocks>
        <models>
            <getname>
                <class>Cpu_CustomGrid_Model</class>
            </getname>
            <cpu_customgrid>
                <class>Cpu_CustomGrid_Model</class>
            </cpu_customgrid>
        </models>
    </global>
<adminhtml>
    <events>
      <adminhtml_block_html_before>
            <observers>
                <addColumnsToSalesOrderGrid>
                    <class>Cpu_CustomGrid_Model_Observer</class>
                    <method>addColumnsToSalesOrderGrid</method>
                </addColumnsToSalesOrderGrid>
            </observers>
        </adminhtml_block_html_before>
    </events>
</adminhtml>    
</config>

Cpu/CustomGrid/Model/Observer.php

class Cpu_CustomGrid_Model_Observer extends Varien_Event_Observer
{   
public function addColumnsToSalesOrderGrid(Varien_Event_Observer $observer)
    {
    $event = $observer->getEvent();
    $this->_grid = $event->getBlock();
    if ($this->_grid instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
        $this->_collection = $this->_grid->getCollection();

        $select = $this->_collection->getSelect();
        $resource = Mage::getSingleton('core/resource');    


        $columnData = array(
                'header' => 'Name',
                'index'  => 'name', // Account Name/Surname
                'type' => 'text',
                'renderer' => 'Cpu_CustomGrid_Block_Getname' 
            );

        $this->_grid->addColumnAfter('name', $columnData, 'company');
        $this->_grid->sortColumnsByOrder();



        // rebuild the filters
        $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);
            }
        }
        // force a reload of the collection
        $this->_collection->load();
        }
    }

}

Cpu/CustomGrid/Block/Getname.php

class Cpu_CustomGrid_Block_Getname extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
         return $row->getFirstname().' '.$row->getLastname();
    }
}

Unfortunately I cannot get the Name + Lastname to show. What could be the problem?

  • Please make print_r to $row and check what all results you are getting – Nihar S Apr 24 '17 at 16:28
1

I guess the problem is that you do not have the "firstname" and "lastname" datas in your collection. You will have to make one or two sql join in order to get it. Unfortunatly, it does not appear to be easy because these data are customer eav attributes.

As your collection is a Mage_Sales_Model_Resource_Order_Collection object, you can take a look to the app/code/core/Mage/Sales/Model/Resource/Order/Collection.php::_addAddressFields method in you want a more precise idea of what I mean (this method joins the collection with the table 'sales/order_address' to get Firstname and Lastname of the order addresses).

I did not test it but you should try to join with the 'customer_entity_varchar' table with something like :

$firstnameAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'firstname');
                $firstnameTable = $firstnameAttribute->getBackendTable();
                $firstnameId = $firstnameAttribute->getAttributeId();

$collection
        ->getSelect()
        ->joinLeft(
            array('cevfirst' => $firstnameTable),
            'main_table.customer_id = cevfirst.entity_id AND cevfirst.attribute_id ='.$firstnameId,
            array(
                        'customer_firstname' => new Zend_Db_Expr('cevfirst.value'),
                    )

You should then be able to get the firstname data with

$row->getCustomerFirstname()

After that, you should repeat the same thing to get the lastname value.

I hope this will help.

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.