Question

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()) ?>" />       
Was it helpful?

Solution

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!!

OTHER TIPS

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 } ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top