English 中文(简体)
How do I filter a collection by a YesNo type attribute?
原标题:
  • 时间:2009-11-10 22:32:35
  •  标签:
  • magento

I have a ‘featured’ attribute, which has a Yes/No select-list as the admin input. I presume that the values for Yes and No are 1 and 0, as they are for every other Yes/No list. However, if I try and filter a collection using the ‘featured’ attribute, it doesn’t work:

$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId(1);

But, if I make a ‘featured’ attribute with a dropdown, and write my own Yes and No, then it works as below:

$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId( Yes );

Anyone any ideas? I’ve also tried values as true/false, yes/no, on/off etc, but no joy.

问题回答

This seems to be an old thread, but anyway I just had the same issue, I set the attribute to be visible in product listing and product view, and then apply addAttributeToFilter(feature_product_attribute, 1) for Yes/No type.

Maybe you are supposed to use 1 and 0 instead of the integer-values?

Like:

$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId( 1 );

Whenever Magento s behavior is confusing me, I start hacking on the core source (a development copy, of course) to see what it s doing and why not doing what I think it should. I haven t done much playing around with the Admin UI stuff so I don t 100% understand your question, but take a look at the getOption function

File: /app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Abstract.php

public function getOptionId($value)
{
    foreach ($this->getAllOptions() as $option) {
        if (strcasecmp($option[ label ], $value)==0 || $option[ value ] == $value) {
            return $option[ value ];
        }
    }
    return null;
}

I d add some Mage::Log and/or var_dump calls in there for the values of $option[ label ] and $option[ value ] and see why your comparison is failing.





相关问题
Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no ...

Get product link from Magento API

I am new to Magento and using their API. I need to be able to get the product url from the API call. I see that I can access the url_key and url_path, but unfortunately that s not necessarily what ...

magento extension installation

I want to install a Magento extension in WAMP, but not from the Magento connect system. How can I do this? I have the module (extension) code and I already installed the sample data in the Magento ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

How do I filter a collection by a YesNo type attribute?

I have a ‘featured’ attribute, which has a Yes/No select-list as the admin input. I presume that the values for Yes and No are 1 and 0, as they are for every other Yes/No list. However, if I try and ...

热门标签