简体   繁体   中英

How to displa a custom attribute value in catalog product grid in magento?

I have created a custom attribute for product "sellable_in_market". And tried to display it in the product grid. But that column is empty. But if I filter with YES/NO then it is displaying. How to display the attribute value ("sellable_in_market") with out filter in grid ?. No clue what to do. Below is my Code.

Advance in thanks.

 protected function _prepareCollection()
        {   
            parent::_prepareCollection();
            $collection = $this->getCollection();
            $store = $this->_getStore();
            if ($store->getId()) {
               $collection = $collection->joinAttribute(
                'sellable_in_market',
                'catalog_product/sellable_in_market',
                'entity_id',
                null,
                'left',
                $store->getId()
                );
            }
            else {

                $collection = $collection->joinAttribute('sellable_in_market', 'catalog_product/sellable_in_market', 'entity_id', null, 'left');
            }
         $this->setCollection($collection);
            return $this;
        }

        protected function _prepareColumns()
        {  
            $this->addColumnAfter('sellable_in_market',
            array(
            'header'=> Mage::helper('catalog')->__('Resellable'),
            'width' => '60px',
            'index' => 'sellable_in_market',
            'sortable'  => true,
            'type'  => 'options',
            'options' => array("1" => 'Yes', "0" => 'No'),
            ),
            'type'
            );
            parent::_prepareColumns();

        }

In the grid "Resellable" column is empty. But if we filter with yes/no then it is displaying. How to diplay a Custom value in grid by default ?

Add below code to select attribuet in _prepareCollection function of product grid Mage_Adminhtml_Block_Catalog_Product_Grid before $this->setCollection($collection) line.

$attributeCode = 'qc_status';//here your attribute code
        $collection->joinAttribute($attributeCode, 'catalog_product/'.$attributeCode, 'entity_id', null, 'left');
        $collection->addAttributeToSelect($attributeCode);

And then below code for column in _prepareColumns function of grid.

$attributeCodeConfig ='qc_status';//Your attribute code...

        $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $attributeCodeConfig);

        $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
        $attributeData = $attribute->getData();
        $frontEndLabel = $attributeData['frontend_label'];

        $attributeOptions = $attribute->getSource()->getAllOptions();
        $b = new Mage_Catalog_Model_Resource_Eav_Attribute();
        $attributeOptions2 = array();
        foreach ($attributeOptions as $value) {
            if(!empty($value['value'])) {
                $attributeOptions2[$value['value']] = $value['label'];
            }

        }


        if(count($attributeOptions2) > 0) {
            $this->addColumn($attributeCodeConfig,
                array(
                    'header'=> Mage::helper('catalog')->__($frontEndLabel),
                    'width' => '80px',
                    'index' => $attributeCodeConfig,
                    'type'  => 'options',
                    'options' => $attributeOptions2,

            ));
        } else {
            $this->addColumn($attributeCodeConfig,
                array(
                    'header'=> Mage::helper('catalog')->__($frontEndLabel),
                    'width' => '80px',
                    'index' => $attributeCodeConfig,

            ));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM