1

I set a custom image role, after uploading and selecting the image as "auszeichnung_1" it will appear on the product page.

However if I do not select an image, I get on frontend this error:

Fatal error: Uncaught Error: Call to a member function getValue() on null in /var/www/hifistudio/hsstage/app/design/frontend/TemplateMonster/theme007/Magento_Catalog/templates/product/view/auszeichnungen.phtml:13 Stack trace: #0 /var/www/hifistudio/hsstage/vendor/magento/framework/View/TemplateEngine/Php.php(59): include() #1 /var/www/hifistudio/hsstage/vendor/magento/framework/View/Element/Template.php(271): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\View\Attributes), '/var/www/hifist...', Array) #2 /var/www/hifistudio/hsstage/vendor/magento/framework/View/Element/Template.php(301): Magento\Framework\View\Element\Template->fetchView('/var/www/hifist...') #3 /var/www/hifistudio/hsstage/vendor/magento/framework/View/Element/AbstractBlock.php(668): Magento\Framework\View\Element\Template->_toHtml() #4 /var/www/hifistudio/hsstage/vendor/magento/framework/View/Layout.php(557): Magento\Framework\View\Element\AbstractBlock->toHtml() #5 /var/www/hifistudio/hsstage/vendor/magento/framew in /var/www/hifistudio/hsstage/app/design/frontend/TemplateMonster/theme007/Magento_Catalog/templates/product/view/auszeichnungen.phtml on line 13

Can someone help me with the right "if-clause"? I used this code in my phtml:

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$productImageAttr = $_product->getCustomAttribute( 'auszeichnung_1' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
->init($_product, 'auszeichnung_1')
->setImageFile($productImageAttr->getValue());
?>
<img src="<?php echo $productImage->getUrl() ?>"width="50" alt="<?php echo $block->escapeHtml($productImage->getLabel()) ?>" />       

2 Answers 2

1

Please used below code

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$productImageAttr = $_product->getCustomAttribute( 'auszeichnung_1' );
if ($productImageAttr ) {
    $productImage = $this->helper('Magento\Catalog\Helper\Image')
                         ->init($_product, 'auszeichnung_1')
                         ->setImageFile($productImageAttr->getValue());
    ?>
    <img src="<?php echo $productImage->getUrl() ?>"width="50" alt="<?php echo $block->escapeHtml($productImage->getLabel()) ?>" /> 
 <?php } ?>

I hope this is helpful to you!!

3
  • Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /var/www/hifistudio/hsstage/app/design/frontend/TemplateMonster/theme007/Magento_Catalog/templates/product/view/auszeichnungen.phtml on line 5 Line 5 is if (isset($productImageAttr->getValue())) { Jul 7, 2020 at 19:47
  • Please check I have update the answer Jul 8, 2020 at 4:06
  • Hi When I create a new product and do not choose the new image role, there are placeholder images in the product page- Very strange. "old" products do not show anything. How can I avoid that? see stage.hifi-studio.at/manratz-sr7015.html Jul 15, 2020 at 19:04
1

Thanks to Rassig Miyani for the right direction!

This Code works for me:

    <?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$productImageAttr = $_product->getCustomAttribute( 'auszeichnung_1' );
if (null !== $_product->getCustomAttribute( 'auszeichnung_1')) {
    $productImage = $this->helper('Magento\Catalog\Helper\Image')
                         ->init($_product, 'auszeichnung_1')
                         ->setImageFile($productImageAttr->getValue());
    ?>
    <img src="<?php echo $productImage->getUrl() ?>"width="50" alt="<?php echo $block->escapeHtml($productImage->getLabel()) ?>" /> 
 <?php } ?>
2
  • Welcome @Benjamin Jul 8, 2020 at 4:14
  • Hi When I create a new product and do not choose the new image role, there are placeholder images in the product page- Very strange. "old" products do not show anything. How can I avoid that? see stage.hifi-studio.at/manratz-sr7015.html Jul 29, 2020 at 7:20

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.