1

I have created some media image attributes to show in a couple of places on the product page.

They work fine if an image is selected for each of them but if not the page doesnt load correctly it seems to stop loading at more information.

enter image description here

If I go into the product and select an image for the attributes, save, then unselect and save again the product opens but can't do this for every product.

It works fine with new text attributes its only empty media image attributes.

This is for magento 2.1.12.

The code for pulling each the attributes is

<div class="col-sm-4">
    <img src="<?php
    $productImageAttr = $product->getCustomAttribute( 'productfeature2' );
    $productImage = $this->helper('Magento\Catalog\Helper\Image')
        ->init($product, 'productfeature2')
        ->setImageFile($productImageAttr->getValue());
    echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" /></p>
</div>

Is there a way to code this to skip any empty attributes from displaying?

1 Answer 1

1

Try to use this code

<div class="col-sm-4">
    <?php $productImageAttr = $product->getCustomAttribute( 'productfeature2');?>
   <?php if($productImageAttr) : ?>
    <img src="<?php
    $productImage = $this->helper('Magento\Catalog\Helper\Image')
        ->init($product, 'productfeature2')
        ->setImageFile($productImageAttr->getValue());
    echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />
    <?php endif;?>
</div>
0

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.