0

I have created a product attribute of type text area. Its typical value will be a unordered list, such as:

<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
</ul>

The attribute code is 'usp' I wish to display this attribute on the product screen and have created a new template for this purpose. When I hard code a value in the template, it displays properly. However, when I put the following

<?php echo $this->getProduct()->getAttributeText('usp') ?>

in the template, I get an error:

Call to a member function getAttributeText() on null in...

How should I code this?

1
  • what page do you need to display attribute value? like product, category or custom page – Bilal Usean Aug 13 '18 at 9:44
1

First you have to use helper to call this method of yours. So try below code:

<?php 
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct(); 
    echo $_helper->productAttribute($_product, $_product->getUsp(), 'usp');
?>

OR

You can assign value of your attribute to any variable and than call that variable wherever you need. see below code:

<?php 
     $_helper = $this->helper('Magento\Catalog\Helper\Output');
     $_product = $block->getProduct(); 
     $usp = $_product->getResource()->getAttribute('usp')->getFrontend()->getValue($_product);
?>

you can use this variable $usp wherever you want.

Another thing is that if you want this attribute for current particular product then you could use below code:

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product 
    $usp = $product->getResource()->getAttribute('usp')->getFrontend()->getValue($product);
?>
2
  • The first suggestion results in this error: Fatal error: Uncaught Error: Call to a member function getUsp() on null in – Amblymoron Aug 13 '18 at 15:49
  • The last suggestion does exactly what I want. Thank you! – Amblymoron Aug 13 '18 at 15:49
0

Use below code inside your new template.

<?php
 $_product=$this->getProduct();
 $_helper = $this->helper('catalog/output');
 echo $_helper->productAttribute($_product, $_product->getUsp(), 'usp');
 ?>

or you can use

<?php echo $this->getProduct()->getUsp() ?>

we use $this->getProduct()->getAttributeText('usp') if attribute is dropdown type. that's why it is not working here.

2
  • Using <?php $_product=$this->getProduct(); $_helper = $this->helper('catalog/output'); echo $_helper->productAttribute($_product, $_product->getUsp(), 'usp'); ?> gives error "Exception #0 (ReflectionException): Class catalog/output does not exist" – Amblymoron Aug 13 '18 at 15:00
  • are you using magento2 or magento1 ? – Ansar Husain Aug 13 '18 at 15:55

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.