7

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.

3 Answers 3

24

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");
4
  • seems i am missing somewhere. please check view.phtml : pastebin.com/RjEnsHwR please search for "brnad" in the code. Feb 9, 2016 at 9:28
  • 1
    you're missing the php echo instruction that will "echo" the text on the screen, I've edited my answer
    – Giuseppe
    Feb 9, 2016 at 9:31
  • And for a multiselect?
    – snh_nl
    Sep 23, 2019 at 12:34
  • Thanks for the getData() :)
    – Meirza
    Apr 27, 2021 at 9:28
5

If I understand your quest correctly you are trying to get attribute value on the product page.

This should give you the result:

$_product->getAttributeText('my_attribute');

Assumption $_product is an object and already initiated before calling the above code.

Check some more info regarding attribute:

/**
 * get attribute collection
 */
$attribute = $_product->getResource()->getAttribute('my_attribute');
/**
 * get attribute type
 */
$attribute->getAttributeType();
/**
 * get attribute Label
 */
$attribute->getFrontendLabel();
/**
 * get attribute default value
 */
$attribute->getDefaultValue();
/**
 * check if the attribute is visible
 */
$attribute->getIsVisible();
/**
 * check if the attribute is required
 */
$attribute->getIsRequired();
/**
 * get attribute value
 */
$attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getMyAttribute();

More reading: http://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-value/

[UPDATE]

To get brand in product view page you have to do this:

  1. Go to admin then catalog > attribute > manage attribute
  2. Then search manufacturer and edit that
  3. Set Visible on Product View Page on Front-end to YES

This will make sure it is visible in the frontend.

8
  • seems i am missing somewhere. please check view.phtml : pastebin.com/RjEnsHwR please search for "brnad" in the code. Feb 9, 2016 at 9:28
  • To get brand you should call manufacturer instead. So it is $_product->getAttributeText('manufacturer'); Feb 9, 2016 at 9:29
  • sorry, i tried that . but it did't worked me.... Feb 9, 2016 at 9:44
  • You need to set Visible on Product View Page on Front-end to Yes to be able to show up in frontend. Have you done this? Feb 9, 2016 at 9:48
  • yes, i done that before , but i wanted the attribute value in some other place, so i was struggling for that .... Feb 9, 2016 at 9:53
0

On product view page

<?php 

echo "brand is " . $_product->getAttributeCode();

?>
1
  • There is no getAttributeCode method on the product model. You may have confused it with the attribute model.
    – Cladiuss
    Jul 26, 2019 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.