cancel
Showing results for 
Search instead for 
Did you mean: 

Use an image URL for custom attribute

Use an image URL for custom attribute

Hi,

 

I have a custom arributre that show the total capacity a product can carry.

 

i.e  maximum 2, maximum 3, maximum 4 etc.

 

I display this attribute using:

 

echo   $_product->getAttributeText('color');

 

My question is, instead of the text, I want an image to show.

 

So if the attribute = maximum 3, it show image3.jpg  etc.

 

Can anyone advise on how to do this?

 

The images have already been uploaded so I guess I need to call a URL somewhere?


Thanks

13 REPLIES 13

Re: Use an image URL for custom attribute

Hello Hayes7888,

 

I think you should create an attribute select. I have two suggestions for you:

1. You should use if else condition, and then upload images into your current skin (call these images by $this->getSkinUrl('images/example-img.png')). (I use rwd package)

 

    <?php $capacity = $_product->getAttributeText('capacity');
        $img = '';
        if($capacity = 'maximum 2'):
            $img = $this->getSkinUrl('images/maximum-2.png');
        //http://<base_url>/skin/frontend/rwd/default/images/maximum-2.png
        elseif($capacity = 'maximum 3'):
            $img = $this->getSkinUrl('images/maximum-3.png');
        else:
            $img = $this->getSkinUrl('images/maximum-4.png');
        endif;
        
        if($img) {
            echo $img;
        }
    ?>

 

2. An advanced level, you build a module:

a) Having a table your_capacity:

+capacity_id

+name

+img

+...

b) Then create an Admin Grid to manage your capacity: Create, Read, Update and Delete your capacity entity, and can work with image file.

c) Create an attribute capacity for product with source model which selects database from your_capacity table

d) ...

There is an example tutorial here.

 

Problem solved? Click Accept as Solution!

Re: Use an image URL for custom attribute

Thanks for the suggestions.

 

I have used the code below.

 

All it seems to do though is echo the URL of  " 1.jpg"  (this images are correctly uploaded where they should be)

 

It doesn't display the image at all, just the URL.

 

Also, this same URL is showing for all products no matter what the attribute is set to.....

 

 

<?php
$capacity = $_product->getAttributeText('color');
$img = '';
if($capacity = 'One'):
$img = $this->getSkinUrl('1.jpg');
elseif($capacity = 'Two'):
$img = $this->getSkinUrl('2.jpg');

elseif($capacity = 'Three'):
$img = $this->getSkinUrl('3.jpg');

elseif($capacity = 'Four'):
$img = $this->getSkinUrl('4.jpg');

endif;

if($img) {
echo $img;
}
?>

 

 

 

 

 

Re: Use an image URL for custom attribute

Hello Hayes7888,

 

When calling $this->getSkinUrl() -> only return the current url skin, for example : http://<base_url>/skin/frontend/rwd/default/ (rwd is current package). So, you must upload your images under folder default. 

 

Note: You should put your images into folder images of current skin, for example: http://<base_url>/skin/frontend/rwd/default/images/your_images.png.

Problem solved? Click Accept as Solution!

Re: Use an image URL for custom attribute

I'm not sure I follow....

 

I have changed the code to reference where the images uploaded but still can't get it to work.

 

if I paste the echo'd URL into a browser the images are loading

 


http://www.mysite.com/skin/frontend/default/theme_name/images/1.jpg

 

<?php  
$capacity = $_product->getAttributeText('color');
$img = '';
if($capacity = 'One'):
$img = $this->getSkinUrl('images/1.jpg');

elseif($capacity = 'Two'):
$img = $this->getSkinUrl('images/2.jpg');
			
elseif($capacity = 'Three'):
$img = $this->getSkinUrl('images/3.jpg');
			
elseif($capacity = 'Four'):
$img = $this->getSkinUrl('images/4.jpg');
									
endif;
        
if($img) {
echo $img;
}
?>

Re: Use an image URL for custom attribute

Hello Hayes7888,

 

Forgive me. Yes, I know only show image:

 

<?php  
$capacity = $_product->getAttributeText('color');
$img = '';
if($capacity = 'One'):
$img = $this->getSkinUrl('images/1.jpg');

elseif($capacity = 'Two'):
$img = $this->getSkinUrl('images/2.jpg');
			
elseif($capacity = 'Three'):
$img = $this->getSkinUrl('images/3.jpg');
			
elseif($capacity = 'Four'):
$img = $this->getSkinUrl('images/4.jpg');
									
endif;
?>  
<?php if($img): ?>
<img src="<?php echo $img; ?>" alt="Capacity"/>
<?php endif;?>

We need an img HTML element to show in front page.

 

 

Problem solved? Click Accept as Solution!

Re: Use an image URL for custom attribute

Oh yeah!

 

Didn't notice that was missing!

 

Ok, it now loads the image, but only 1.jpg

 

it does not change using the elseif 

 

 

Re: Use an image URL for custom attribute

 

You should change value of custom attribute. Go to admin, Catalog > Manage Products > choose a product > change value custom. Then, go to this product front page.

Problem solved? Click Accept as Solution!

Re: Use an image URL for custom attribute

I have,

 

using

 

<?php echo   $_product->getAttributeText('color') ?>

 

shows that the attributes are correct.

 

Just the images that are not equaling the correct attribute that has been set

Re: Use an image URL for custom attribute

Hello Hayes7888,

 

Did you change the value of custom attribute in Admin? You must change the value of custom attribute of product.

Problem solved? Click Accept as Solution!