繁体   English   中英

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

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

我为产品“ sellable_in_market”创建了一个自定义属性。 并尝试将其显示在产品网格中。 但是该列为空。 但是,如果我使用“是/否”进行过滤,则会显示出来。 如何在网格中没有过滤器的情况下显示属性值(“ 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();

        }

在网格中“可转售”列为空。 但是,如果我们选择“是/否”进行过滤,则会显示出来。 默认情况下如何在网格中显示自定义值?

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

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

            ));
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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