Magento:获得自定义属性文本的管理网格的类别>产品标签网格


Magento: Get custom attribute text on Admin Grid of Category > Product Tab Grid

我使用以下代码将自定义属性拉入Admin Catalog> Product Tab网格。

$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'
    ));

显示属性id,例如92,97,95。这不是很用户友好,所以我想知道如何才能获得属性的实际名称/标签。

在前端我会使用:

 $_product->getAttributeText('pos_product_type') 

显示标签,但是我不能在后端转换它

您可以在Magento代码中找到答案,例如检查可见性:

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

还可以检查与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,
));

这很容易实现:https://github.com/magento-hackathon/GridControl

编辑

你想要的是添加选项到你的列:

$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')
));
我应该复制辅助函数:
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;       
}

感谢webguys: http://www.webguys.de/magento/turchen-23-pimp-my-produktgrid/

但是你不懂德语,是吗?

我们正在接近建议,但最终的解决方案来自以下:

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

出于性能考虑,您可能希望使用MySQL而不是PHP来过滤属性代码。随着时间的推移,属性和选项的列表可能会变得相当大,不需要循环遍历它们。

修改后的示例如下:

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