0

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

Thanks

  • 1
    try this $_product->getData('your_attribute'); – Rakesh Donga Mar 28 at 7:01
  • 1
    Make sure the attribute exist for the product you're getting, guess that would be the issue @shiivashankarm – Prathap Gunasekaran Mar 28 at 7:04
  • Thanks @PrathapGunasekaran issue with value only now the value is getting. – shivashankar m Mar 28 at 7:16
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 – Bhargava Rama Mar 28 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> – shivashankar m Mar 28 at 7:36
  • but it is calling for newly created products previosly existing products it is not displaying – shivashankar m Mar 28 at 7:37
  • Can you share the Vendor\Module\Block\Product\View\test block code? – Bhargava Rama Mar 28 at 8:08
  • Please find the link to block code: magento.stackexchange.com/questions/267766/… – shivashankar m Mar 28 at 8:21

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.