16

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?

6 Answers 6

25

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

2
  • it not works if attribute is a media image type.
    – LucScu
    Commented Oct 18, 2017 at 9:43
  • @Bilal, for me custom attributes are showing "fatal error getFrontend" in list.phtml. can you tell please how to avoid this error please if you have any idea Commented Jan 3, 2019 at 13:11
14

I created an article about this, that have only 5 steps to do it:

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');

if ($_getMyAttr){

    // 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.

Reference: https://rafaelstz.github.io/magento/magento2-display-custom-attribute-catalog-list-products.html

4
  • With media image attribute ”Visible on Product View Page on Front-end” and “Used in Product Listing” are not settable.
    – LucScu
    Commented Oct 18, 2017 at 9:44
  • same for price attribute Commented Dec 13, 2017 at 19:02
  • where can I find the default list.phtml ? Commented Jan 9, 2018 at 18:48
  • hello, attribute label is working for me but not attribute value Commented Aug 29, 2018 at 12:41
6

Use this

$attribute = $_product->getResource()->getAttribute('identifier'); 
if ($attribute) 
{ 
$attr_value = $attribute ->getFrontend()->getValue($_product); 
}
1

@mikebertiean To find the default list.phtml navigate to

<Magento base>/vendor/magento/module-catalog/view/frontend/templates/product

Once you've copied it and altered it and added it to the path that Rafael mentioned, you should just need to flush your cache and it'll work.

1

since the $_product->getResource() has been deprecated I tried to find another solution and I found a simpler way to display the product attribute:

$_product->getAttributeText('manufacturer')

Here is the original response: https://magento.stackexchange.com/a/297442/95081

1
  • Thanks! This should be the new accepted answer. Commented May 13, 2022 at 3:58
0

Open

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

Add the following between foreach loop.

<?php

$brand_attribute = $_product->getResource()->getAttribute('brand');

if ($brand_attribute){

    // Get Value
    $brand_value = $brand_attribute->getFrontend()->getValue($_product);

    // Get Label
    $brand_lable = $brand_attribute->getStoreLabel();
    echo $brand_lable." : ".$brand_value;
}

?>

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.