1

Default catalog_product_prices.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
                <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
                <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
                <item name="default_amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item>
                <item name="prices" xsi:type="array">
                    <item name="special_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                    </item>
                    <item name="tier_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item>
                    </item>
                    <item name="final_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item>
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item>
                    </item>
                    <item name="custom_option_price" xsi:type="array">
                        <item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item>
                    </item>
                    <item name="configured_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\ConfiguredPriceBox</item>
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/configured_price.phtml</item>
                    </item>
                </item>
                <!--<item name="adjustments" xsi:type="array"></item>-->
            </argument>
        </arguments>
    </block>
</layout>

My catalog_product_prices.xml to display attribute below product price and above Add to Cart button.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>      
        <referenceBlock name="render.product.prices">
            <block class="Reward\Points\Block\Points" name="reward_points" template="points.phtml" />
        </referenceBlock>
    </body>
</page>

Reward\Points\Block\Points

<?php
namespace Reward\Points\Block;

class Points extends \Magento\Framework\View\Element\Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

I tried with above code, but its not working. How do I display custom attribute value in product listing page ?

7 Answers 7

2

This is another way to call, In your list.phtml file after price code add below code,

<?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getMyattr(), 'myattr') ?>

Make sure from attribute option it is visible for product listing page from admin.

enter image description here

2
  • what is getMyattr()?
    – jtate
    Oct 24, 2016 at 22:01
  • It is attribute code that i had created from admin using attribute creation page. my attribute code was myattr so when i wanted to call it i used, $_product->getMyattr() .
    – Rushvi
    Oct 25, 2016 at 5:36
2

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

1

have you checked if the "use in product listing" property of the attribute is set to 'yes'? Further, try reindexing if required.

0
1

use this

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

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

This is the most simple way that I have found.

In your list.phtml between the foreach function, paste this code.

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

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

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

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

1

In product list page you can display like this.

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

using the helper as per one of the answers above is the recommended way:

  1. the helper is already used in the code code; therefore you follow the lead from the core team this way (meaning if the core team adds something in this function, your code is benefiting from it right away)
  2. without using helper, you will have code that is potentially not secure: looking at the helper function you can see it escapes the data that are parts of the rendering

in short:

<?php $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?>
<?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getCustomAttribute(), 'custom_attribute') ?>

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.