7

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?

3 Answers 3

19

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");
}
5
  • This will get the ID of the attribute by it's code, not the ID of the attribute option by it's label :) Jun 24, 2015 at 8:29
  • 1
    Check my updated code Jun 24, 2015 at 8:38
  • 1
    Nice, very few lines of code Jun 24, 2015 at 9:16
  • @Manashvibirla: How I can make $attr = $_product->getResource()->getAttribute("color"); dynamic? Sometimes I will sent attribute labels from Color section, sometimes from Manufacturer, etc.
    – amitshree
    Jun 24, 2015 at 9:44
  • check updated ans Jun 24, 2015 at 10:02
5

There's not really a simple method to call that does that but this should do the trick

$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'color');

$values = Mage::getResourceModel('eav/entity_attribute_option_collection')
    ->setAttributeFilter($attribute->getId())
    ->addFieldToFilter('tsv.value', 'Bleu')
    ->setStoreFilter(Mage::app()->getStore()->getId(), false)
    ->addFieldToSelect('option_id');
$values->getSelect()->limit(1);

var_dump($values->getFirstItem()->getId());

Note that the value has to be exactly the same as the label, so including capital letters etc.

3

Get attribute option value

 getOptionvalue('your attribute code like color','your attribute label like blue');

    function getOptionvalue($arg_attribute, $arg_value) 
        {
            $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $arg_attribute);

            foreach ( $attribute->getSource()->getAllOptions(true, true) as $option )
            {

                if($arg_value == $option['label'])
                {

                    unset($attribute);
                    return $option['value'] ; 
                }
            }
3
  • Is it possible to get $arg_attribute dynamically there because $arg_value will be changing( ex: sometimes it will come form color section, sometimes from Manufacturer).
    – amitshree
    Jun 24, 2015 at 10:05
  • Yes, call function getOptionvalue('color',blue); or getOptionvalue('manufacture','some value');
    – Mufaddal
    Jun 24, 2015 at 10:06
  • 1
    Looping through all options is not a good idea - especially if you have a lot of them.
    – Simon
    Jan 19, 2016 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.