Magento 1.8 – How to Add Column in Customer Grid with Dropdown Values

magento-1.8

I've added a custom attribute for customers to choose from during registration. For simplicity sake lets say this is their favorite color (color_customer). Values are pulled from a dropdown list. Everything works fine in both front- and backend. I added this custom attribute to the customer grid by overwriting the grid file. I then tried to get the values of the color attribute before the _prepareColumns function. I'm not so sure this is the right way to go and I'm hoping someone could shed some light on this matter.

    <?php
    class Company_Module_Block_Adminhtml_Customer_Grid extends      Mage_Adminhtml_Block_Customer_Grid{

    protected function _prepareCollection(){
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->addAttributeToSelect('color_customer')
        ->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);

    if ($this->getCollection()) {

        $this->_preparePage();

        $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
        $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
        $filter   = $this->getParam($this->getVarNameFilter(), null);

        if (is_null($filter)) {
            $filter = $this->_defaultFilter;
        }

        if (is_string($filter)) {
            $data = $this->helper('adminhtml')->prepareFilterString($filter);
            $this->_setFilterValues($data);
        }
        else if ($filter && is_array($filter)) {
            $this->_setFilterValues($filter);
        }
        else if(0 !== sizeof($this->_defaultFilter)) {
            $this->_setFilterValues($this->_defaultFilter);
        }

        if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
            $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
            $this->_columns[$columnId]->setDir($dir);
            $this->_setCollectionOrder($this->_columns[$columnId]);
        }

        if (!$this->_isExport) {
            $this->getCollection()->load();
            $this->_afterLoadCollection();
        }
    }

    return $this;
     }
     }


protected function _prepareColumns(){
   $color = Mage::getModel('eav/config')->getAttribute('customer', 'color_customer');
   $options = $color->getSource()->getAllOptions(false);
   $colors = array();
   foreach ($options as $option)
   {
       $colors[$option['value']] = $option['label'];
   }
    $this->addColumnAfter('color_customer', array(
        'header'    => Mage::helper('customer')->__('Fav Color'),
        'width'     => '80px',
        'index'     => 'color_customer',
        'type'      => 'options',
        'options'   => $colors
    ),'email');
    return parent::_prepareColumns();
  }

Best Answer

You can get the options like this:

$color = Mage::getModel('eav/config')->getAttribute('customer', 'color_customer');
$options = $color->getSource()->getAllOptions(false);

You seam to have it figured out for the rest of it.

Related Topic