Magento产品网格筛选器与自定义属性

3
我已经在管理区域中的产品网格中添加了自定义属性,使用以下代码位于 /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php 函数_prepareColumns() 中。
它可以正常工作,但是现在当使用任何搜索过滤器进行搜索时,新属性列不显示任何值。
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','custom_column');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
$attributeOptions = $attribute->getSource()->getAllOptions();

$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
    if(!empty($value['value'])) {
        $attributeOptions2[$value['value']] = $value['label'];
    }
}

$this->addColumn('custom_column',
    array(
        'header'=> Mage::helper('catalog')->__('Custom Column'),
        'width' => '150px',
        'index' => 'custom_column',
        'type'  => 'options',
        'options' => $attributeOptions2,
));

在_prepareCollection()方法中,我添加了以下代码:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('custom_column');

我觉得这很简单,但我暂时没有理解,非常感谢您的帮助!
编辑:
当使用过滤器进行搜索时,该列会填充值,除非过滤器是名称列。

你没有尝试在目录中使用“管理属性”吗?在那里,你可以简单地将“在快速搜索中使用”字段设置为是! - DRAJI
试试这个扩展:https://github.com/tzyganu/GridEnhancer。它是免费的,可以让你管理很多管理员网格而不需要编写任何代码。 - Marius
@Marius,你的扩展看起来非常棒,我已经检查了源代码并进行了测试,但不幸的是它没有解决问题。实际上,我的代码在使用除名称列以外的任何列进行筛选时都会显示值,但是你的扩展根本不显示新自定义列的任何值。 - Ethan Kawkji
您解决了这个问题吗?请发布答案。我也遇到了同样的麻烦。 - Pavan Kumar
2个回答

2
我不得不在 _prepareCollection 中添加以下内容:
        $collection->joinAttribute(
            'custom_column',
            'catalog_product/custom_column',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );

0
尝试添加自定义的过滤器回调函数。
$this->addColumn('custom_column',
  array(
    'header'=> Mage::helper('catalog')->__('Custom Column'),
    'width' => '150px',
    'index' => 'custom_column',
    'type'  => 'options',
    'options' => $attributeOptions2,
    'filter_condition_callback' => array($this, 'filter_custom_column_callback'),
));

在那里定义您的筛选查询,就像这个例子中一样:

protected function filter_custom_column_callback($collection, $column)
{
    $filterValue = $column->getFilter()->getValue();
    $collection->getSelect()->where(" ... ");
    return $this;
}

谢谢您的建议@maglater,但是 - 您能否提供更详细的答案,说明$collection->getSelect()->where(" ... ")中的查询应该是什么样子的? 我已经尝试了许多组合,但到目前为止都没有成功。 - Ethan Kawkji
请尝试使用 $collection->addAttributeToFilter('custom_column', $filterValue) 替换 $collection->getSelect()->where(" ... "); 的写法。 - freento
尝试使用 $collection->addAttributeToFilter('custom_column', $filterValue) 代码,但结果仍然相同。当使用“名称”列进行过滤时,新列不显示值,而其他列正常运作。这个特定列为什么会有过滤新添加列的问题?有任何想法吗? - Ethan Kawkji
在 $collection->addAttributeToFilter('custom_column', $filterValue); 之后添加 echo $collection->getSelect(); die; 并检查过滤期间它尝试执行的 SQL 查询。 - freento

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接