Magento 2: Display custom attribute in catalog list products


This customization is very common in creating a theme for Magento 2, and several developers use more complex paths that are not required to achieve the same result. This step-by-step will help you in 5 simple steps.

Step-by-step

1 - Create your attribute in Stores > Attributes > Product.

2 - Set the attribute in the Default group, go to Stores > Attributes > Attribute Set.

3 - Set the values of this attributes in your products.

4 - Open in your custom theme the relative file below:

app/design/frontend/CUSTOM/THEME/Magento_Catalog/templates/product/list.phtml

5 - Between the foreach function paste this code below, but changing the code of your correct attribute:

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

// Get Value
$attrTestValue = $_getMyAttr->getFrontend()->getValue($_product);

// Get Label
$attrTestLabel = $_getMyAttr->getStoreLabel();

Important

Make sure “Visible on Product View Page on Front-end” and “Used in Product Listing” options under storefront Properties are set to Yes.