Magento – Get Attribute Value/ID from Attribute Text/Label

attributesfilterable-attributes

I've a requirement to show attribute labels in URL to filter products. I'm able to generate Url with attribute labels. Now all I've to do is convert attribute label to attribute id/value (ex: 1,2), so that code behind the scene works as it is (based on attribute value/id).

Within apply() function of app/code/core/Mage/Catalog/Model/Layer/Filter/Attribute.php I'm getting attribute label in $filter = $request->getParam($this->_requestVar); (ex: Blue).

How I can convert this attribute label to attribute id (say for green it's 2), so that I can pass it to $this->_getResource()->applyFilterToCollection($this, $filter); and backend functionality will work as it is?
I've tried $filter->getId(), $filter->getValue() but it doesn't work.

Is it possible to get attribute value/id from attribute label?

Best Answer

Get attribute value/id from attribute text/label:

$attr = 'your_attribute';
$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute($attr);
if ($attr->usesSource()) {
    echo $color_id = $attr->getSource()->getOptionId("Purple");
}
Related Topic