Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display multi-option customer attribute within customer management admin grid

Tags:

magento

Okay so with Customer Attributes I have a multi-option selection that I have added to the Manage Customers Grid.

    $prodCode = Mage::getSingleton('eav/config')->getAttribute('customer','prod_codes');
    $prodCodeOptions = $prodCode->getSource()->getAllOptions(false);
    $prodOptions = array();

    foreach($prodCodeOptions as $k)
        $prodOptions[$k['value']] = $k['label'];

    $this->addColumn('prod_codes', array(
        'header'    =>  Mage::helper('customer')->__('Product Code'),
        'width'     =>  '100',
        'index'     =>  'prod_codes',
        'type'      =>  'options',
        'options'   =>  $prodOptions,
        'filter_condition_callback'
                    => array($this, '_filterProdOptionsCondition'),
    ));

I do have my attribute added to the collection at the top of my Grid.php:

->addAttributeToSelect('prod_codes')

Here is my _filterProdOptionsCondition method:

protected function _filterProdOptionsCondition($collection, $column) {
    if(!$value = $column->getFilter()->getValue()) {
        return;
    }
    $this->getCollection()->addFieldToFilter('prod_codes', array('finset' => $value));
    #print($collection->getSelectSql());
}

Now this work fine and dandy if I only have ONE of the options selected, once I apply more than one option to the customers attribute I will get a blank results within the admin grid, however it IS still searchable.

A close look with the print($collection->getSelectSql()); uncommented I see that the attribute ID values are being returned in an comma delimited list.

Now onto my question with that background laid out, is there a method or "Magento" way to display these multi-options within the admin grid I'm just unaware of? Or do I need to simply get the comma values exploded and call for a new collection to build out the display values? Any help appreciated!

like image 439
B00MER Avatar asked May 25 '11 21:05

B00MER


1 Answers

Appears I had to extend the Column renderer to anticipate comma values and simply render them, I'm amazed this isn't built in since the functionality exists to create the multioptions attributes but no grid display option for it.

app/code/local/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Options.php

public function render(Varien_Object $row)
{
    $options = $this->getColumn()->getOptions();
    $showMissingOptionValues = (bool)$this->getColumn()->getShowMissingOptionValues();
    if (!empty($options) && is_array($options)) {
        $value = $row->getData($this->getColumn()->getIndex());
        if (is_array($value)) {
            $res = array();
            foreach ($value as $item) {
                if (isset($options[$item])) {
                    $res[] = $options[$item];
                }
                elseif ($showMissingOptionValues) {
                    $res[] = $item;
                }
            }
            return implode(', ', $res);
        }
        elseif (isset($options[$value])) {
            return $options[$value];
        } elseif (is_string($value)) { // <--- MY CHANGES HERE
            $values = explode(',', $value);
            $returnOptions = "";
            foreach($values as $k=>$v) {
                $returnOptions .= $options[$v]. ", ";
            }
            return substr($returnOptions, 0, -2);
        }
        return '';
    }
}
like image 68
B00MER Avatar answered Sep 30 '22 16:09

B00MER