0

Magento 2 how to get custom attribute title and its value in phtml file.

  • which type of custom attribute is it ? product,customer and where do you want to get value ( page ) – Naveed Asim Sep 11 '18 at 11:23
  • Its just custom attribute i need to display its title and value in product detail page.. – trilok kumar Sep 11 '18 at 11:27
  • what type of attribute is it ? text, select etc ? – Naveed Asim Sep 11 '18 at 11:27
  • Text attribute... – trilok kumar Sep 11 '18 at 11:29
  • blog.chapagain.com.np/… just visit this link – Mukesh Prajapati Sep 11 '18 at 11:39
3

As per your comments, I assume that you are getting the product object.

Then use:

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

This will give the attribute object, where attribute_code is the attribute code.

then use the following code to get the value of attribute for current product:

$attribute->getFrontend()->getValue($_product);

and use below code to get the name (label) of the attribute:

$attribute->getFrontendLabel();
| improve this answer | |
1

Go to :

Store->Attributes->Product

find your attribute and click to open edit page and select "Storefront Properties"

And from there "Allow HTML Tags on Storefront" select to Yes.

Then on your template you can get value by $product->getYourAttributeCode()

NOTE : if your attribute is like custom_attribute then your method will be like $product->getCustomAttribute();

| improve this answer | |
1

You can get attribute label and attribute value as below

$myAttr = $_product->getResource()->getAttribute('my_attribute');

if ($myAttr){

    // Get Value
    $attrValue = $myAttr->getFrontend()->getValue($_product);

    // Get Label
    $attrLabel = $myAttr->getStoreLabel();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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