【问题标题】:Magento products grid filter with custom attribute具有自定义属性的 Magento 产品网格过滤器
【发布时间】:2013-12-11 03:18:06
【问题描述】:

我已使用 /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');

我认为这很简单,但我现在没有理解,非常感谢任何帮助!

编辑:

当使用过滤器进行搜索时 - 该列会填充值,除非过滤器是“名称”列。

【问题讨论】:

  • 您没有尝试使用目录中的管理属性吗?在那里你可以简单地将“在快速搜索中使用”字段设置为是!
  • 试试这个扩展:github.com/tzyganu/GridEnhancer。它是免费的,它允许您在不编写任何代码的情况下管理大量管理网格。
  • @Marius,您的扩展看起来非常好,我检查了源代码并对其进行了测试,但不幸的是它没有解决问题。事实上,我上面的代码在过滤除 Name 列之外的任何列时都会显示值,但您的扩展程序根本没有显示新自定义列的任何值。
  • 你解决了这个问题吗?请张贴答案。我有同样的麻烦

标签: php magento filter grid admin


【解决方案1】:

我必须将以下内容添加到_prepareCollection

        $collection->joinAttribute(
            'custom_column',
            'catalog_product/custom_column',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );

【讨论】:

    【解决方案2】:

    尝试添加自定义过滤回调

    $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(" ... ") 的查询提供更详细的答案?我已经尝试了很多组合,但到目前为止没有任何运气。
    • 试试 $collection->addAttributeToFilter('custom_column', $filterValue);而不是 $collection->getSelect()->where(" ... ");
    • 试过 $collection->addAttributeToFilter('custom_column', $filterValue);还是一样。新列仅在使用“名称”列过滤时不显示值,其他列工作正常。知道为什么此特定列在过滤新添加的列时存在问题吗?
    • $collection->addAttributeToFilter('custom_column', $filterValue);添加回声 $collection->getSelect();死;并检查它在过滤期间尝试执行的 SQL 查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    相关资源
    最近更新 更多