11

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?

17

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

  • it not works if attribute is a media image type. – LucScu Oct 18 '17 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 – hitesh balpande Jan 3 at 13:11
8

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

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

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.

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.