Magento – Magento 2: I can’t display the value of a custom product attribute in product page

attributescustommagento2pageproduct

I want to show two product custom attributes in product view page side by side the price with the same format (font size).
The attributes are PP and PR witch I created in Magento Admin and assign to the product.

enter image description here

Next I tried to put the code in list.phtml to display the attributes:

enter image description here

I used

<?php echo 'PP ' . $_product->getData('pp') . ' PR ' . $_product->getData('pr'); ?>

But unfortunately I just can't retrieved the value of the attributes and it shows blank.
enter image description here

Could someone help me on this issue?

Best Answer

Try below code.

<?php /* @escapeNotVerified */ echo 'PP '.$_product->getResource()->getAttribute('pp')->getFrontend()->getValue($_product) . ' PR ' .     $_product->getResource()->getAttribute('pr')->getFrontend()->getValue($_product)?>

Also, check both attributes are Visible on Catalog Pages on Storefront Used in Product Listing.

Related Topic