如何在magento中的目录产品网格中显示自定义属性值?

时间:2016-08-02 07:42:09

标签: php magento

我为产品“sellable_in_market”创建了自定义属性。并试图在产品网格中显示它。但那个专栏是空的。但如果我用YES / NO过滤,那么它就会显示出来。如何在网格中使用out过滤器显示属性值(“sellable_in_market”)?不知道该怎么做。以下是我的代码。

谢谢你。

 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();

        }

在网格中“Resellable”列为空。但是如果我们用yes / no过滤那么它就会显示出来。 如何在默认情况下在网格中显示自定义值?

1 个答案:

答案 0 :(得分:0)

添加以下代码,在产品网格的_prepareCollection函数中选择attribuet Mage_Adminhtml_Block_Catalog_Product_Grid在$ this-> setCollection($ collection)行之前。

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

然后在网格的_prepareColumns函数中的列的代码下面。

$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,

            ));
        }