Magento: addAttributeToFilter but ignore for products that don't have this attribute?

旧巷老猫 提交于 2019-12-10 09:37:03

问题


I'm trying to show add some filters on my store, but they have a nasty side effect.

Suppose I have product type A and B. Now I want to only show A where color = blue/red.

$collection = Mage::getResourceModel('catalog/product_collection')
    ->setStoreId($this->getStoreId())
    ->addCategoryFilter($this)
    ->addAttributeToFilter(array(
          array('attribute' => 'color', 'in' => array(4, 6)),
    )
    );

This does the trick, but now because product type B has no value assigned to color(since this attribute isn't appointed to it) no products fo this type show up.

I had found this code on the forum http://www.magentocommerce.com/boards/viewthread/178309 , but it doesn't work:

array('attribute' => 'color', 'is' => new Zend_Db_Expr('null'))

Neither does:

array('attribute' => 'color', 'null' => true),

That actually shows products wich have the attribute assigned but with no value declared ...

I also tried adding:

array('attribute' => 'price', 'gteq' => 0), 

Because I figured these statements were connected with 'OR' (according to the documentation) but even that only adds product types wich have the attribute assigned ...

Note that these values come from a drop down list, not sure if that matters though.


回答1:


Maybe it's too late, but this works for me:

$collection = Mage::getResourceModel('catalog/product_collection')
    ->setStoreId($this->getStoreId())
    ->addCategoryFilter($this)
    ->addAttributeToFilter(
        array(
            array('attribute' => 'color', 'null' => true),
            array('attribute' => 'color', 'in' => array(4, 6)),
        ),
        '',
        'left'
);



回答2:


Offhand, give this a shot:

$collection = Mage::getResourceModel('catalog/product_collection')
    ->setStoreId($this->getStoreId())
    ->addCategoryFilter($this)
    ->addAttributeToFilter(array(array('attribute' => 'color', 'in' => array(4, 6)),'left')
);


来源:https://stackoverflow.com/questions/3403850/magento-addattributetofilter-but-ignore-for-products-that-dont-have-this-attri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!