【问题标题】:How to displa a custom attribute value in catalog product grid in magento?如何在magento的目录产品网格中显示自定义属性值?
【发布时间】:2023-03-09 07:45:01
【问题描述】:

我为产品“sellable_in_market”创建了一个自定义属性。并尝试在产品网格中显示它。但那一栏是空的。但是如果我用 YES/NO 过滤,那么它就会显示出来。如何在网格中显示不带过滤器的属性值(“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”列是空的。但是如果我们用是/否过滤,那么它就会显示出来。 如何在网格中默认显示自定义值?

【问题讨论】:

  • 您是否在产品列表页面中打印了产品数组?
  • 是的,没问题。我可以在那里看到“sellable_in_market”。
  • 您在管理网格上的收藏中获得了价值吗?
  • 我正在获取集合中的值而不是网格中的值。

标签: php magento


【解决方案1】:

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

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

            ));
        }

【讨论】:

    猜你喜欢
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多