0

I already loaded few custom attributes on the product list view. Now I want to show images instead of the attribute values.

I tried:

$filename= $_SERVER['DOCUMENT_ROOT'].'/media/catalog/brands/'.str_replace(' ', '_',$brand).'.jpg';
if (file_exists($filename)) {
      echo '<img style="float: left; margin: 2px;" src="/media/catalog/brands/'.str_replace(' ', '_',$brand).'.jpg" alt="'.$brand.'">'; 
  } 

but it did not work.

3 Answers 3

1

I dont understand. The product images are already being displayed in the category.

See: /app/design/frontend/base/default/template/catalog/product/view.phtml

<?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
1
  • I want to load attribute icon as not image , here is the example how i cons should look snag.gy/5zTfP.jpg , i don,t want to replace product image with attribute image
    – Zuby
    Commented Dec 22, 2015 at 10:58
0

You are trying to show custom attribute image if available

You need to code like below

    <?php 
$product = Mage::getModel('catalog/product')->load($_product->getId());
$brand = $product->getData('attribute-code-here'); 
$filename = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/brands/'.str_replace(' ', '_',$brand).'.jpg';

    if(@getimagesize($filename)){ ?>
        <img src="<?php echo $filename; ?>" alt="" />
    <?php } ?>
1
  • I want to load attribute icon as not image , here is the example how i cons should look snag.gy/5zTfP.jpg , i don,t want to replace product image with attribute image
    – Zuby
    Commented Dec 22, 2015 at 10:58
0

You should use

Mage::getBaseDir('media')

instead of

$_SERVER['DOCUMENT_ROOT'].'/media'

and in the image src attribute

Mage::getBaseUrl('media')

instead of

/media

If it still does not work, and $brand actually contains the attribute value, then the images are obviously not in the place where you are looking for them.

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.