Wednesday 3 February 2016

How to get custom attribute value in Magento

Display custom attribute value in Magento


When we get custom attribute value in Magento through simple way like:



<?php 


echo $_product->getAttributeText('custom_attribute_code');


?>


Then we will get a fatal error (as following) , if the custom attribute is not already added. 


" Fatal error: Call to a member function getSource() on a non-object in ...\app\code\core\Mage\Catalog\Model\Product.php on line 1388"


So we will use following code for get custom attribute value in Magento.

The following code will first check that the custom attribute are added or not in admin. If custom attribute are added then get its value.




<?php 

$attribute = $_product->getResource()->getAttribute('custom_attribute_code');

         if ($attribute)

          {

               echo $attribute_value = $attribute ->getFrontend()->getValue($_product);

          }

?>




7 comments:

  1. Well done! This post is too good. Thanks author for giving this kind of posts.
    Keep updating.
    Galvanised Sheets In Haryana 

    ReplyDelete
  2. This is the proper information about the custom attribute value, which is really well maintained and it has very nice information. Keep updating your posts.
    moisturizing hand care lotion

    ReplyDelete
  3. Very impressive information which is helpful and pretty well maintained. Thanks for sharing, keep updating.
    Price Of Hoverboard In India

    ReplyDelete
  4. Really great to know about the attribute, thanks for it.
    Buy jammu blue sapphire online

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete