Magento 1.8 – How to Get Image Based on Sort Order

ce-1.9.0.1magento-1.8

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

Best Answer

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

Related Topic