Get attribute value in product view page

  • I want to to display attribute value in Product view page. link

    I'm trying following code :

    echo $_attribute->getProductAttribute()->getAttributeCode();
    

    or

    $_attribute_code = Mage::getModel('eav/entity_attribute')->load($_attribute->getAttributeId())->getAttributeCode();
    

    I replaced attribute code : "brand" in above code and using :

     echo $_attribute->getProductAttribute()->getbrand();
    

    or

     $_brand = Mage::getModel('eav/entity_attribute')->load($_attribute->getAttributeId())->type();
    

    but it's not working : Fatal error: Call to a member function getAttributeId() on a non-object in

    Please help me to find where i am missing.

  • Giuseppe

    Giuseppe Correct answer

    5 years ago

    If you are in the product view page, in the catalog/product/view.phtml there is an instance of the product model usually called $_product.

    From this variable you have access to all the attribute products by the getter methods.

    If you want to display the value on the page you have to use the echo php command.

    echo "Brand is " . $_product->getData("attribute_code");
    or
    echo "Brand is " . $_product->getAttributeCode();
    

    If you have a "dropdown" attribute type getData() and it will return the id of the option. If you need the label of the dropdown you can use the

    echo "Brand is " . $_product->getAttributeText("attribute_code");
    

    seems i am missing somewhere. please check view.phtml : http://pastebin.com/RjEnsHwR please search for "brnad" in the code.

    you're missing the php echo instruction that will "echo" the text on the screen, I've edited my answer

    And for a multiselect?

    Thanks for the getData() :)

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM