0

I'm trying to show a custom image and if it doesn't exist to show the regular image and not a placeholder. No matter what I try so far I get image not found exception error. Here is one of my attempts:

    <?php $product = Mage::registry('current_product'); ?>
    <?php $custom_image = $this->helper('catalog/image')->init($product, 'custom_image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE); ?>
    <?php $regular_image = $this->helper('catalog/image')->init($product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE); ?>            
    <?php if ($custom_image != ''): ?>
    <?php echo $custom_image; ?>
    <?php else: ?>
    <?php echo $regular_image; ?>
    <?php endif; ?>

As long as there is a image it works.

0

Try this :

<?php if($product->getCustomImage()):?>
   <?php $custom_image =  Mage::helper('catalog/image')
    ->init($product,'custom_image')->constrainOnly(TRUE)
    ->keepAspectRatio(TRUE)->keepFrame(FALSE); ?>
   <?php echo $custom_image; ?>
 <?php else: ?>
    <?php $regular_image =  Mage::helper('catalog/image')
     ->init($product,'image')->constrainOnly(TRUE)
     ->keepAspectRatio(TRUE)->keepFrame(FALSE); ?>
    <?php echo $regular_image; ?>
<?php endif; ?>
7
  • I tried it and get the same exception error on line (14) image not found. Line 14 is <?php $custom_image = Mage::helper('catalog/image')->init($product, 'custom_image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE); ?> I guess because it's outside the if statement. – Freejoy Jul 22 '18 at 15:02
  • i wanna know what is the custom_image that u are trying to show. It's related to product ? – sami23 Jul 22 '18 at 16:21
  • Yes, it's a image I have in admin under product images. Special image for Pinterest. But many items don't have it. – Freejoy Jul 22 '18 at 16:29
  • @Freejoy i just edit my answer try it and tell me if it works – sami23 Jul 22 '18 at 17:16
  • I tried it and get this error: PHP Fatal error: Uncaught Error: Call to a member function getPinterestImage() on null . – Freejoy Jul 22 '18 at 18:15

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.