2

I want to show a custom attribute of a customer in the adminhtml customer grid. I managed to do this by creating a new module, and extends the setCollection method of Mage_Adminhtml_Block_Customer_Grid

public function setCollection($collection)
    {
        $collection->addAttributeToSelect('x_customer_category');
        parent::setCollection($collection);
    }

And I add the column via observer to the view

/**
     * Adds column to admin customers grid
     *
     * @param Varien_Event_Observer $observer
     *
     * @return Mscg_TemplateOverwrite_Model_Customer_Observer
     */
    public function addCustomerGroupToGrid(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();
        if (!isset($block)) {
            return $this;
        }

        if ($block->getType() == 'adminhtml/customer_grid') {
            /* @var $block Mage_Adminhtml_Block_Customer_Grid */
            $block->addColumnAfter('x_customer_category', array(
                'header' => 'x_customer_category',
                'type'   => 'text',
                'index'  => 'x_customer_category',
            ), 'email');
        }
    }

However, since this is a option-wise attribute I only get the id of the selected value for the customer but I do want the textvalue, not the entity value of given attribut.

How can I do that?

enter image description here

2

You need to change your column type from text to options and add an options element that contains your values in this format key => value.
Something like this:

$block->addColumnAfter('x_customer_category', array(
            'header' => 'x_customer_category',
            'type'   => 'options',
            'index'  => 'x_customer_category',
            'options' => array(
                    'id1' => 'label 1',
                    'id2' => 'label 2',
                ),
        ), 'email');

If the possible values are dynamic you should first build an array with all the values. You should know a way to retrieve all the possible values and just assign the array you get to the options element.
If your attribute is a standard select attribute you can build it like this:

$attribute = Mage::getModel('eav/config')->getAttribute('customer', 'x_customer_category');
$options = $attribute->getSource()->getAllOptions(false);
$values = array();
foreach ($options as $option){
    $values[$option['value']] = $option['label'];
}

Then add your column like this:

 $block->addColumnAfter('x_customer_category', array(
            'header' => 'x_customer_category',
            'type'   => 'options',
            'index'  => 'x_customer_category',
            'options' => $values,
        ), 'email');
0

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.