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!
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With