Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve attribute code

Tags:

magento

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!

like image 396
Hervé Guétin Avatar asked Jul 27 '10 08:07

Hervé Guétin


People also ask

How do I get attribute code?

You can use anywhere this code by inject \Magento\Eav\Model\Entity\Attribute class in your construct. After that, you can call getCustomAttributeCode() function to get attribute code.

Which method is used to retrieve the values of the attributes?

First, declare an instance of the attribute you want to retrieve. Then, use the Attribute. GetCustomAttribute method to initialize the new attribute to the value of the attribute you want to retrieve. Once the new attribute is initialized, you simply use its properties to get the values.

How can we fetch all attributes for an HTML element?

To get all of the attributes of a DOM element: Use the getAttributeNames() method to get an array of the element's attribute names. Use the reduce() method to iterate over the array. On each iteration, add a new key/value pair containing the name and value of the attribute.


1 Answers

No need to overload Magento base code in this case. If you print_r($_attribute) you can see Attribute Code is deeper in array() and you can use it directly in you template file this way:

echo $_attribute->getProductAttribute()->getAttributeCode();
like image 60
demijohn Avatar answered Nov 29 '22 01:11

demijohn