无法通过type_id过滤产品集合

时间:2013-11-09 06:57:47

标签: magento

我正在观察 catalog_product_collection_load_before 事件并尝试根据其type_id过滤产品集合。但是,我一直在未找到列:1054未知列'e.type_id'在'where子句错误。

代码是这样的:

$observer->getCollection()->addFieldToFilter(array(
    array(
        'attribute' => 'price',
        'eq'      => '20',
        ),
    array(
        'attribute' => 'type_id',
        'neq'       => 'simple',
        ),
    ));

我甚至试图让它更简单,但仍然不起作用。

$observer->getCollection()->addFieldToFilter('type_id','simple');

它适用于其他属性,例如price,name,entity_id,但不适用于type_id。那是为什么?

3 个答案:

答案 0 :(得分:2)

在集合中尝试这段代码。

$collection->getSelect() ->joinInner(array(’cpe’ => ‘catalog_product_entity’),’e.entity_id = cpe.entity_id’) ->where("cpe.type_id = ‘simple’");

不要使用addAttributeToFilter,因为在通过价格进行过滤时,主表格已成为catalog_product_index_price,因此我们可以通过type_id进行过滤,以上代码效果很好对我来说。

请试一试。

答案 1 :(得分:0)

如果适用于您,请尝试以下代码:

$observer = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'))->load();

答案 2 :(得分:0)

尝试以下代码,它可以帮助您:

$observer = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'))
    ->addFieldToFilter(
        array(
          array(
           'attribute' => 'price',
           'eq'      => '20',
          )
        )
    )
    ->load();
相关问题