Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Get attribute option ID from value
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

Hi!

I have the following situation: For our online-bookshop I have created an attribute called author. Now, I would like to be able to have a link on any product-page. E.g. on the product-page of a book by J.N. Darby I want to have the author’s name converted to a link to the advanced search result page, for author J.N. Darby.

The problem is that I need the ID for the option to achieve that. I know, how to get the option value via getAttributeText(’author’), but how do I get the corresponding ID?

Can anybody help me with that?

Thanks in advance!

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

doesn’t anyone have any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
subesh
Sr. Member
 
Avatar
Total Posts:  145
Joined:  2009-01-08
 

will this help you

// Add the attribute code here.
$attribute=$product->getResource()->getAttribute("color");

// Checking if the attribute is either select or multiselect type.
if($attribute->usesSource()){

    
// Getting all the sources (options) and print as label-value pair
    
$options $attribute->getSource()->getAllOptions(false);

    echo 
"<pre>";
    
print_r($options);
}

More details on my blog site.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

Hi!

I’ll have a look at it later (I just had a very exhausting day trying to fly through the snow in europe). For the moment: Thank you very much for answering!

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

Hi!

This looks very promising but something seems to be wrong:

When I implement this I get a lot of meaningless symbols. Here is an excerpt:

‹¾"Ó“šâUU™ae¦B>I( …Ì3oéYëÎ,QYµîýúQÆÃÀ|«Ð�Ÿ´‰lÞ•?{zŸ+Ÿn¡¢L}+8� ʅÿ�“f³ùôù« w7¡?ùº2Øg냾{D¿Æ·~–�aB¥‡ÉEïÃ5aâ ýÕhèüèÑëÅ4ŠÔZ‹aXmÊL

what does that mean?

 
Magento Community Magento Community
Magento Community
Magento Community
 
oneal
Jr. Member
 
Total Posts:  7
Joined:  2009-06-10
Santpedor (Barcelona)
 

Same problem here!

Few hours searching and got the solution for me. Hope this helps…

<?=$_product->getData('manufacturer');?>

Now you can use this id for advanced searching. I’m doing the same but for manufacturers using this url:

http://www.mysite.com/category.html?manufacturer=id

Got products page filtered by this manufacturer id.

bye!

 
Magento Community Magento Community
Magento Community
Magento Community
 
subesh
Sr. Member
 
Avatar
Total Posts:  145
Joined:  2009-01-08
 
nanos - 23 December 2009 08:07 AM

Hi!

This looks very promising but something seems to be wrong:

When I implement this I get a lot of meaningless symbols. Here is an excerpt:

‹¾"Ó“šâUU™ae¦B>I( …Ì3oéYëÎ,QYµîýúQÆÃÀ|«Ð�Ÿ´‰lÞ•?{zŸ+Ÿn¡¢L}+8� ʅÿ�“f³ùôù« w7¡?ùº2Øg냾{D¿Æ·~–�aB¥‡ÉEïÃ5aâ ýÕhèüèÑëÅ4ŠÔZ‹aXmÊL

what does that mean?

Where did you run that..? Run that on cron.php.. inside try block and exit after the code..to check...if it works or not!

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

@oneal:

Thank you very much! That’s exactly what I was looking for!

@subesh: I put it into the /app/design/frontend/default/c2default/template/catalog/product/view/attributes.phtml file

from what I deduce from your comment that seems to have been the wrong location of the code.

Anyway, oneal’s solution seems to be perfect.

Thanks again for you help, both of you! It’s really appreciated!

 
Magento Community Magento Community
Magento Community
Magento Community
 
felo
Jr. Member
 
Total Posts:  25
Joined:  2010-11-10
Durham, NC
 

Hi subesh,
that got me exactly where I wanted!
I have another problem however that maybe you can help me.
I need to load a customer address attribute but based on the admin value I set to it in the backend.
when I run your code, I get the array with all its values and labels but can’t find the value set in the admin. Do you know what I mean with that?
thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
felo
Jr. Member
 
Total Posts:  25
Joined:  2010-11-10
Durham, NC
 

Well, I got something going although I doubt this is the best way to go… here it is:
apparently the admin values are stored in another store (which would happen to be the admin one)
What I managed to do is change the store to load the data and then switch back.

// SET TO ADMIN STORE
        Mage::app()->getStore()->setId(0);
        
$add Mage::getModel('customer/address')->load($order->getShippingAddress()->getId());
        
$attribute $add->getResource()->getAttribute("district");
        
$district $attribute->getSource()->getOptionText($order->getShippingAddress()->getData('district'));
// SET BACK TO DEFAULT STORE // NOTE THAT YOUR STORE MAY DIFFER FROM MINE IF YOU ARE USING MORE THAN ONE
        
Mage::app()->getStore()->setId(1);

Hope this helps someone, and please if you come up with a better way, let me know!

 
Magento Community Magento Community
Magento Community
Magento Community
 
jazkat
Sr. Member
 
Total Posts:  154
Joined:  2009-07-09
 
nanos - 23 December 2009 08:07 AM

Hi!

This looks very promising but something seems to be wrong:

When I implement this I get a lot of meaningless symbols. Here is an excerpt:

‹¾"Ó“šâUU™ae¦B>I( …Ì3oéYëÎ,QYµîýúQÆÃÀ|«Ð�Ÿ´‰lÞ•?{zŸ+Ÿn¡¢L}+8� ʅÿ�“f³ùôù« w7¡?ùº2Øg냾{D¿Æ·~–�aB¥‡ÉEïÃ5aâ ýÕhèüèÑëÅ4ŠÔZ‹aXmÊL

what does that mean?

If you’re getting strange characters it sometimes means that Magento doesn’t recognize variables.
I’m guessing the reason in this case is a “$product” variable.
So change subesh’s first line a bit
from

$attribute=$product->getResource()->getAttribute("color");

to

$attribute=Mage::getModel('catalog/product')->getResource()->getAttribute("color");

And thanks Subesh for your help and your always helpful blog!

cheers,
jazkat

 
Magento Community Magento Community
Magento Community
Magento Community
 
kunjesh
Jr. Member
 
Total Posts:  17
Joined:  2010-03-08
 

Thank You subesh,

Your example really help me out.

Thanks a lot from,
Kunjesh Sukhadia

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianzig
Jr. Member
 
Total Posts:  3
Joined:  2008-09-22
 

Thanks! Worked fine for me on 1.6.  I appreciate it.  It really saved me some time.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top