tags:

views:

35

answers:

3

Hi,

A very small thing that is driving me crazy :)

I am not able to retrieve the attribute code of the current attribute in the /catalog/product/view/type/options/configurable.phtml template file.

Around (or at) line 36, when I change the

echo $_attribute->getLabel()

to

echo $_attribute->getAttributeId()

I get the correct attribute Id that is present in the eav_attribute table of the database. But when I try

echo $_attribute->getAttributeCode()

I get an empty string whereas there is an attribute_code field in the eav_attribute table.

Can you please help me find the attribute code of my attribute ? Or, more generally : how to get the attribute code of an attribute from which we now the id ?

Thanks a lot!

A: 

I just tried that code on my magento installation and it returns the correct attribute code. You can always try getting to the data directly:

echo $_attribute->attribute_code;

but I don't think this will help. Try var_dump or print_r on the $_attribute variable to see if the data is really missing.

silvo
Thanks man for answering Silvo !But echo $_attribute->attribute_code didn't return nothing either and the print_r returned something that made me think that the attribute code is encapsulated into another array that I didn't find a way to point dynamically. Anyway, found a sooooo strange solution that I've posted below.
vrnet
A: 

So found a way which I'm sure is not the best way to do... or maybe Magento team didn't think that one may need to get the attribute code based on attribute id...

1- Overload app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Collection.php

Add this method :

public function setIdFilter($id)
    {
        if (empty($id)) {
            return $this;
        }
        if (!is_array($id)) {
            $id = array($id);
        }
        $this->getSelect()->where('main_table.attribute_id IN(?)', $id);
        return $this;
    }

2- Now to retrieve the code of the attribute based on its id use :

$attributeCode = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setIdFilter(YOUR_ATTRIBUTE_ID) /* if using this into the configurable.phtml template file, you can use $_attribute->getAttributeId() for YOUR_ATTRIBUTE_ID */
        ->getFirstItem()
        ->getAttributeCode();
echo $attributeCode;

So unituitive but so working...

If someone's got a better way to do it, I'd be happy to change my "answer ticker"!

vrnet
A: 

Here is detail code and explanation about getting attribute name, code and value : http://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-value/

chapagain
Hey Mukesh, I've seen your post prior to posting by own answer :) So funny. But I didn't think that the last snippet would do the trick... So I took inspiration from it to make my own which, as you saw, needs to overload the main Collection.php file...Anyway, you got the ticker!
vrnet