Question

I'm using the following code to pull a custom attribute into the Admin Catalog> Product Tab Grid.

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('image')
        ->addAttributeToSelect('pos_product_type')

+

 $this->addColumn('pos_product_type', array(
        'header'    => Mage::helper('catalog')->__('OsiPos Category'),
        'sortable'  => true,
        'width'     => '80',
        'index'     => 'pos_product_type'
    ));

This shows the attribute id, eg 92, 97, 95. This isn't very user friendly so I'm wondering how I could go about getting the actual name / label of the attribute.

On the frontend I would use:

 $_product->getAttributeText('pos_product_type') 

to display the label but I can't convert the it on the back end.

Was it helpful?

Solution 3

We were getting close with the suggestions but the final solution came from the following:

// Add Pos Product Type

    $pos_items = 
    Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');
            foreach ($pos_items as $pos_item) :
                if ($pos_item->getAttributeCode() == 'pos_product_type')
                    $pos_options[$pos_item->getOptionId()] = $pos_item->getValue();
            endforeach;

            $this->addColumn('pos_product_type',
                array(
                    'header'=> Mage::helper('catalog')->__('Pos Product Type'),
                    'width' => '100px',
                    'type'  => 'options',
                    'index' => 'pos_product_type',
                    'options' => $pos_options
            ));

OTHER TIPS

You can find your answers at Magento code, check for example visibility:

$this->addColumn('visibility',
    array(
        'header'=> Mage::helper('catalog')->__('Visibility'),
        'width' => '70px',
        'index' => 'visibility',
        'type'  => 'options',
        'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
));

Also you can check code for attribute set, which communicate with db.

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
    ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
    ->load()
    ->toOptionHash();

$this->addColumn('set_name',
    array(
        'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
        'width' => '100px',
        'index' => 'attribute_set_id',
        'type'  => 'options',
        'options' => $sets,
));

This is easy achievable with: https://github.com/magento-hackathon/GridControl

EDIT

What you want is to add the options to your column:

$this->addColumn('pos_product_type', array(
    'header'    => Mage::helper('catalog')->__('OsiPos Category'),
    'sortable'  => true,
    'width'     => '80',
    'index'     => 'pos_product_type',
    'options' => $this->_getProductAttributeOptions('pos_product_type')
));

I should copy the helper function with:

protected function _getProductAttributeOptions($attributeName) {
    $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$attributeName);
    /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */       
    $attributeOptions = $attribute->getSource()->getAllOptions();
    $options = array();
    // options in key => value Format bringen
    foreach ($attributeOptions as $option) {
        $options[number_format($option['value'], 4, '.', '')] = $option['label'];
    }       
    return $options;       
}

Thanks to the webguys: http://www.webguys.de/magento/turchen-23-pimp-my-produktgrid/

But you don't understand german, do you?

You might want to filter the attribute code using MySQL instead of PHP performance sake. The list of attributes and options can get pretty big over time, no need to loop through them all.

Below a modified example:

$pos_items = Mage::getModel('eav/entity_attribute_option')
            ->getCollection()
            ->setStoreFilter()
            ->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code')
            ->addFieldToFilter('attribute_code', 'pos_product_type');

   foreach ($pos_items as $pos_item) :
                $pos_options[$pos_item->getOptionId()] = $pos_item->getValue();
        endforeach;

$this->addColumn('pos_product_type',
            array(
                'header'=> Mage::helper('catalog')->__('Pos Product Type'),
                'width' => '100px',
                'type'  => 'options',
                'index' => 'pos_product_type',
                'options' => $pos_options
        ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top