tags:

views:

97

answers:

2

Hi All

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.

A: 

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

Like:

$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId('1');
CodeMonkey
Tried that too! I think I've tried every possible variation so far, but no joy.
Sam
A: 

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.

Alan Storm
I already tried this as well - I did a vardump and the value says it's boolean, but shows false regardless of whether it's set to true or not, so I'm really stumped!
Sam
Also I checked in Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Boolean and the values listed are 0 and 1. Is it something to do with the way I'm performing the filter perhaps? Works for dropdowns but maybe it needs to be done differently for a Yes/No?
Sam
Is this a Yes/No that you setup yourself in the interface? Maybe eav business is missing some key bit of information when you set it up. That would be my next guess if it was my problem. Here's how I'd debug it. Dump the contents of your database using mysqldump (non-binary) Make a change to one of the Yes/No menus that works, and make another database dump. Diff the two files to see what's changed. Then, track down the similar areas of the database for your problem menu item and see if anything looks "wrong". (vague and tedious, but that's an undocumented system for you)
Alan Storm
No, if I set up a dropdown myself as a Yes/No then it works fine, but if I use the Magento default Yes/No dropdown it doesn't work. I'm starting to think it might be a bug. For now I'm gonna stick with using my own dropdown I think. I've already spent 3 days on this so I think I'm gonna give in and move on to the next Magento headache!
Sam
One last suggestion. Have you tried changing the value of the input back and forth between no and yes and then trying the filtering again? I've seen situations where an attribute has an "unset' value, but the default is displayed on the form.
Alan Storm
Yeah tried that too. I also tried filtering for items with the attribute set to No, and still nothing, so I'm out of ideas now.
Sam