I have created one custom attribute through code, in admin product edit page,but i wolud like to display that value in product detail page,

How to get the custom attribute collection in product detail page magento2.

i have tried with the below code but it is giving an error:

$_attributeValue = $_product->getResource()->getAttribute ($_code)->getFrontend()->getValue($_product);

Fatal error: Uncaught Error: Call to a member function getFrontend() on boolean in 

Help me thankful


  • 1
    try this $_product->getData('your_attribute'); Commented Mar 28, 2019 at 7:01
  • 1
    Make sure the attribute exist for the product you're getting, guess that would be the issue @shiivashankarm Commented Mar 28, 2019 at 7:04
  • Thanks @PrathapGunasekaran issue with value only now the value is getting. Commented Mar 28, 2019 at 7:16

1 Answer 1

$_attributeValue = $_product->getCustomAttribute($_code)

Will give the custom attribute value if $_product is repository object.

If it is a model object you can refer to @Rakesh Donga's comment

  • If your issue is solved. Please accept this as answer. @shiivashankarm Commented Mar 28, 2019 at 7:19
  • Hi @bhargav, iam calling custom phtml file to product view page like the code: <referenceContainer name="product.info.form.content" > <block class="Vendor\Module\Block\Product\View\test" name="reward_frontend_msg" template="Vendor_Module::product/view/test.phtml" before="product.info.addtocart"/> </referenceContainer> Commented Mar 28, 2019 at 7:36
  • but it is calling for newly created products previosly existing products it is not displaying Commented Mar 28, 2019 at 7:37
  • Can you share the Vendor\Module\Block\Product\View\test block code? Commented Mar 28, 2019 at 8:08
  • Please find the link to block code: magento.stackexchange.com/questions/267766/… Commented Mar 28, 2019 at 8:21

Your Answer

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

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