0

I am trying to change the image on hover for product in listing page for one of our Magento Eshop. I can change the image on hover. What i did was i used small and thumbnail image for it and changed it using mouseenter and mouseleave event.

But now i want to change it based on sort order. Say the image with Sort Order 2 should be visible on hover.

I am not sure how to do it ? Could anyone please guide me on this. From where shall i start ?

Thanks

4 Answers 4

1

I was able to solve it. I am pasting the code below for what i did.

Step 1 : Created an helper file and have added this code in there

<?php
class Greeting_Products_Helper_Data extends Mage_Core_Helper_Abstract
{
    function altImage($product, $val, $w, $h, $imgVersion='small_image'){
    $product->load('media_gallery');
    $column = 'position';
    $value = $val;
    $gal = $product->getMediaGalleryImages();
    if ($gal = $product->getMediaGalleryImages())
    {
        if ($altImg = $gal->getItemByColumnValue($column, $value))
        {
            return
            '<img class="alt-img lazyOwl"  style="display:none;" src="' . $this->getImg($product, $w, $h, $imgVersion, $altImg->getFile()) . '" alt="' . $product->getName() . '" />';

        }
    }
    return '';
}

}
?>

Please review the code. It will pretty much explain everything.

To call the alternative image use the following code :

echo $this->helper('greeting_products')->altImage($_product,2,220,220);

Here 2 is the position of image and rest are the image parameters

0

Follow the below code you can got the all detail of your media Gallery with sort id. Then after you can manage as per your requirement.

 <?php 
     $prod=Mage::getModel('catalog/product')->load(1);
     foreach ($this->getGalleryImages() as $_image){
        print_r($_image));    
     }
 ?>

Hope you got the proper Solution.

1
  • i guess you are talking about getMediaGalleryImages()... i tried it... but the thing is how can i get the URL of the image whose position is say 2?... could you please let me know on it
    – Dexter
    Commented May 29, 2014 at 8:54
0

here total solution: first all gallery images of products:

$Product=Mage::getModel("catalog/product")->load(51);
$ImageColl=$Product->getMediaGallery('images');

Second: exclude disabled images from gallery image collection and sort by default position

$sorter=array();
$ret=array();
$key='position_default';
 reset($ImageColl);

    foreach ($ImageColl as $ii => $image) {
        if($image['disabled']){
        continue;
        }
        $sorter[$ii]=$image[$key];
    }
    asort($sorter);

third: to get second gallery image:

$j=0;
$secondimg='';
foreach ($sorter as $ii => $va) {

    $ret[$ii]=$ImageColl[$ii];
    if($j==1){
        $secondimg=$ImageColl[$ii];
    }
    if($j>1){
        break;
    }
    $j++;
}

and $secondimg gives second image

  echo "<pre>";
print_r($secondimg);
echo "</pre>"; 

enter image description here

0

You may use built in Magento's functionality

$_subImages = $_product->getMediaGalleryImages('images')->setOrder('position','ASC');
foreach ($_subImages as $imgKey => $subimage) {
    #Your code 
}

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.