1

I'm trying to add a "Company" column in the Customer Grid. The Company is a Customer Attribute I've added programatically and stored in the DB in the customer_entity_varchar table.

I've rewritten the Grid like this :

class MyCompany_MyModule_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareColumns()
{
    $this->addColumnAfter('company_name', array(
        'header' => 'Company Name',
        'type' => 'text',
        'index' => 'company_name',
    ), 'email');  

    parent::_prepareColumns();
}

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->addAttributeToSelect('company_name')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $this->setCollection($collection);

    return parent::_prepareCollection();
}
}

There is nothing displayed except when I sort or filter with this new company column. For example if I sort with ID, no companies displayed, if I sort with Company, everything is perfect.

I've tried to replace the "return parent::_prepareCollection();" by "return $this;" : everything is displayed as expected but there is not any filter or sort available.

Where is my problem ?

Thanks.

1

Here is the solution of my problem : I was extending the wrong class. Instead of Mage_Adminhtml_Block_Customer_Grid, I extend now Mage_Adminhtml_Block_Widget_Grid. The problem here is I need to add all the functions in Mage_Adminhtml_Block_Customer_Grid in my rewritten class.

Please note that this is the same class as Mage_Adminhtml_Block_Customer_Grid, but with only 2 modifications :

  • addAttributeToSelect in _prepareCollection() function

  • addColumn in _prepareColumns() function

Like this :

class Mycompany_Mymodule_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
    parent::__construct();
    $this->setId('customerGrid');
    $this->setUseAjax(true);
    $this->setDefaultSort('entity_id');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->addAttributeToSelect('company_name')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('entity_id', array(
        'header'    => Mage::helper('customer')->__('ID'),
        'width'     => '50px',
        'index'     => 'entity_id',
        'type'  => 'number',
    ));
    /*$this->addColumn('firstname', array(
        'header'    => Mage::helper('customer')->__('First Name'),
        'index'     => 'firstname'
    ));
    $this->addColumn('lastname', array(
        'header'    => Mage::helper('customer')->__('Last Name'),
        'index'     => 'lastname'
    ));*/
    $this->addColumn('name', array(
        'header'    => Mage::helper('customer')->__('Name'),
        'index'     => 'name'
    ));
    $this->addColumn('email', array(
        'header'    => Mage::helper('customer')->__('Email'),
        'width'     => '150',
        'index'     => 'email'
    ));

    $this->addColumn('company_name', array(
        'header' => 'Company Name',
        'type' => 'text',
        'index' => 'company_name',
    ));

    $groups = Mage::getResourceModel('customer/group_collection')
        ->addFieldToFilter('customer_group_id', array('gt'=> 0))
        ->load()
        ->toOptionHash();

    $this->addColumn('group', array(
        'header'    =>  Mage::helper('customer')->__('Group'),
        'width'     =>  '100',
        'index'     =>  'group_id',
        'type'      =>  'options',
        'options'   =>  $groups,
    ));

    $this->addColumn('Telephone', array(
        'header'    => Mage::helper('customer')->__('Telephone'),
        'width'     => '100',
        'index'     => 'billing_telephone'
    ));

    $this->addColumn('billing_postcode', array(
        'header'    => Mage::helper('customer')->__('ZIP'),
        'width'     => '90',
        'index'     => 'billing_postcode',
    ));

    $this->addColumn('billing_country_id', array(
        'header'    => Mage::helper('customer')->__('Country'),
        'width'     => '100',
        'type'      => 'country',
        'index'     => 'billing_country_id',
    ));

    $this->addColumn('billing_region', array(
        'header'    => Mage::helper('customer')->__('State/Province'),
        'width'     => '100',
        'index'     => 'billing_region',
    ));

    $this->addColumn('customer_since', array(
        'header'    => Mage::helper('customer')->__('Customer Since'),
        'type'      => 'datetime',
        'align'     => 'center',
        'index'     => 'created_at',
        'gmtoffset' => true
    ));

    if (!Mage::app()->isSingleStoreMode()) {
        $this->addColumn('website_id', array(
            'header'    => Mage::helper('customer')->__('Website'),
            'align'     => 'center',
            'width'     => '80px',
            'type'      => 'options',
            'options'   => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
            'index'     => 'website_id',
        ));
    }

    $this->addColumn('action',
        array(
            'header'    =>  Mage::helper('customer')->__('Action'),
            'width'     => '100',
            'type'      => 'action',
            'getter'    => 'getId',
            'actions'   => array(
                array(
                    'caption'   => Mage::helper('customer')->__('Edit'),
                    'url'       => array('base'=> '*/*/edit'),
                    'field'     => 'id'
                )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
    ));

    $this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
    $this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
    return parent::_prepareColumns();
}

protected function _prepareMassaction()
{
    $this->setMassactionIdField('entity_id');
    $this->getMassactionBlock()->setFormFieldName('customer');

    $this->getMassactionBlock()->addItem('delete', array(
         'label'    => Mage::helper('customer')->__('Delete'),
         'url'      => $this->getUrl('*/*/massDelete'),
         'confirm'  => Mage::helper('customer')->__('Are you sure?')
    ));

    $this->getMassactionBlock()->addItem('newsletter_subscribe', array(
         'label'    => Mage::helper('customer')->__('Subscribe to Newsletter'),
         'url'      => $this->getUrl('*/*/massSubscribe')
    ));

    $this->getMassactionBlock()->addItem('newsletter_unsubscribe', array(
         'label'    => Mage::helper('customer')->__('Unsubscribe from Newsletter'),
         'url'      => $this->getUrl('*/*/massUnsubscribe')
    ));

    $groups = $this->helper('customer')->getGroups()->toOptionArray();

    array_unshift($groups, array('label'=> '', 'value'=> ''));
    $this->getMassactionBlock()->addItem('assign_group', array(
         'label'        => Mage::helper('customer')->__('Assign a Customer Group'),
         'url'          => $this->getUrl('*/*/massAssignGroup'),
         'additional'   => array(
            'visibility'    => array(
                 'name'     => 'group',
                 'type'     => 'select',
                 'class'    => 'required-entry',
                 'label'    => Mage::helper('customer')->__('Group'),
                 'values'   => $groups
             )
        )
    ));

    return $this;
}

public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current'=> true));
}

public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
}
}
0

it should field name to company from company_name ANd

it address field and you need add ->joinAttribute() function

0
 class MyCompany_MyModule_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
 {
    protected function _prepareColumns()
    {

        $this->addColumnAfter('company', array(
            ...  
        parent::_prepareColumns();
        //see http://stackoverflow.com/questions/8941460/magento-grid-column-position    

    }

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ...
            ->addAttributeToSelect('company')
            ..

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }
  }

A better way to accomplish this (without rewrite) would be to use an observer

Take a look at Adding a column to the customers grid in Magento admin. Alternative way

  • My attribute is really company_name and the usage of the Observer does exactly the same problem : Company Name is displayed only when I sort or filter with this column ... – guillaume_shm Oct 16 '14 at 7:14

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.