Magento 2 Custom Attributes – How to Display Custom Attribute in Catalog

attributescustom-attributesmagento2

I’ve created a new textfield attribute, with the following settings set to yes:

Visible on Catalog Pages on Storefront
Used in Product Listing

The attribute is assigned to an attribute set and it shows up in the product view as intended.

However it’s not visible in the catalog view, I’m using the default Luma theme and I’ve cleared cache and re-indexed. If I view the template file:

magento-catalog/view/frontend/templates/product/list.phtml

I can’t find any code getting custom attributes, so the Luma theme doesn’t seem to support this by default.

How can I get a products custom attribute label and value in list.phtml?

Best Answer

We can get custom attribute value like below

Attribute Value

<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product); ?>

Label

$address =$_product->getResource()->getAttribute('c_address')->getStoreLabel();

Note : in above line c_address is my custom attribute code.

Reference: Magento/Catalog/templates/product/view/attribute.phtml

Related Topic