0

I have created a counfigurable product with. New Attribute Set & it's attributes. I have set the value in Admin also.

On vendor\magento\module-catalog\view\frontend\templates\product\list.phtml

I'm customizing listing. I'm trying to get my newly added attribute using

foreach ($_productCollection as $_product):
    echo $_helper->productAttribute($_product, $_product->getYear(), 'year');

    $attribute = $_product->getResource()->getAttribute('year');
    if ($attribute) {
        echo $labelValue = $attribute->getFrontend()->getValue($_product);
    }
endforeach;

But it's still not displaying set value. I'm able to see value in Admin Product Page. But not in the frontend.

I have cleared cache as well.

3

0 down vote Replace below code :

$attribute = $_product->getResource()->getAttribute('year');

to my Code

$_attributeValue =$_product->getResource()->getAttribute('year')->getFrontend()->getValue($_product);

remove your Code use my code only one you have to use this will work

After Editing the code run the below command:

php bin/magento cache:flush` & `php bin/magento cache:clean
  • Hi @Ankit By applying this it only display 'No' as a value for some not 2 all. – Ankit Shah Sep 27 '16 at 1:47
  • It's working fine on Product Detail Page, but not in Listing – Ankit Shah Sep 27 '16 at 3:21
  • 1
    @Ankit Shah : what is your Catalog Input Type for Store Owner : In my case i checked with Text field Attribute its working fine also you have to set Under Strore Front while creating Attribute : Visible on Catalog Pages on Storefront => Yes screencast.com/t/htGirHZ4OcO . If you created drop down attribute then only your value is showing No this is because you have not selected Yes . so by default it will show No – Brad Hodge Sep 27 '16 at 4:39
  • 1
    Hi @Ankit:- It's missing Used in Product Listing to Yes which is set now :) – Ankit Shah Sep 27 '16 at 4:52
  • So to display your Custom/Newly created Attribute on Frontend Allow HTML Tags on Storefront,Visible on Catalog Pages on Storefront, Used in Product Listing set to be Yes – Ankit Shah Sep 27 '16 at 4:58
0

Edit the attribute, under Storefront Properties tab make sure you have set Used in Product Listing to "Yes". Otherwise you won't be able to get it's value in list.phtml. Then you can use

$_product->getData('attr_id')

for text fields or

$_product->getAttributeText('attr_id')

for dropdowns or multi select fields.

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.