Output Product Attribute in Magento

6 May 2015| Post by Graham

AuthorGraham

You can output product attributes in Magento using the following code :

[cc lang=”php”] getResource()->getAttribute(‘my_attribute’)) : ?>
getFrontend()->getValue($_product);?>

[/cc]

I have seen a lot of tutorials using code similar to the below :

[cc lang=”php”] getResource()->getAttribute(‘my_attribute’)->getFrontend()->getValue($_product);?>
[/cc]

This is bad practice in my opinion because if the attribute was to ever get deleted from the Magento interface by mistake then it could cause a “Call to getFrontend on a non object” PHP error, potentially breaking the page and hindering the users experience of the website. It is always good to write defensive code that will handle unexpected errors.

Scroll