将制造商属性添加到magento admin产品网格视图

时间:2016-08-22 06:57:17

标签: magento

我使用了以下代码

$manufacturer_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()
    ->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');

    foreach ($manufacturer_items as $manufacturer_item) :
        if ($manufacturer_item->getAttributeCode() == 'manufacturer')
        $manufacturer_options[$manufacturer_item->getOptionId()] = $manufacturer_item->getValue();
    endforeach;

    $this->addColumn('manufacturer',
        array(
        'header'=> Mage::helper('catalog')->__('Manufacturer'),
        'width' => '100px',
        'type'  => 'options',
        'index' => 'manufacturer',
        'options' => $manufacturer_options,
        ));

但我的网格中没有显示任何选项值。 帮助Magento V1.7 Grid View - Add manufacturer attribute to view

2 个答案:

答案 0 :(得分:0)

您的选项数组似乎不包含有效的属性选项。请尝试以下方法:

$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'manufacturer');
$options = array();
if ($attribute->usesSource()) {
    foreach ($attribute->getSource()->getAllOptions(false) as $option) {
        if ($option['value'] != '') {
            $options[$option['value']] = $option['label'];
        }
    }
}

$this->addColumn('manufacturer', array(
    ...
    'options' => $options,
));

另外,请确保您的收藏集包含制造商属性值。

...
->addAttributeToSelect('manufacturer')
...

祝你好运!

答案 1 :(得分:0)

按照渲染网格的实际类中的示例进行操作。该课程为Mage_Adminhtml_Block_Catalog_Product_Grid

尝试将该属性加入集合:

$collection->joinAttribute(
    'manufacturer',
    'catalog_product/manufacturer',
    'entity_id',
    null,
    'left',
    Mage_Core_Model_App::ADMIN_STORE_ID
);

将列添加到网格中:

$attribute  = Mage::getResourceModel('catalog/product')->getAttribute('manufacturer');
$preOptions = $attribute->getSource()->getAllOptions(false);

$options = array();
foreach($preOptions as $option) {
    if($option['value']) {
        $options[$option['value']] = $option['label'];
    }
}

$this->addColumn('manufacturer', array(
     'header'  => $this->__($attribute->getFrontendLabel()),
     'width'   => '100px',
     'type'    => 'options',
     'index'   => $attribute->getAttributeCode(),
     'options' => $options,
));