0

I currently have a extension that sets a hover on an image when ever it finds a label that is called 'hover'. I wish to change this to hover an image if there is an image with sort order id which is '2'. But I do not know how to do this. The code is below.

    $html = "";
    $imgcount = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages()->count();
    if ($imgcount>0):
        $_gallery = Mage::getModel('catalog/product') -> load($_product -> getId()) -> getMediaGalleryImages();
        foreach ($_gallery as $_image ):
            if ($_image->getLabel() == 'hover'):
                $html = '<span class="hover-image"><img ' . $this->getImgSources($_product, $imgType, $w, $h, $_image -> getFile()) . ' alt="" /></span>';
            break;
            endif;
        endforeach;
    endif;
    return $html;

I was hoping it would be as simple as if ($_image->getSortOrder() == 2): but that was just flawed thinking. Any ideas?

3
  • Is this from media.phtml? Commented Oct 7, 2014 at 16:58
  • Try getDefaultPosition() Instead of sortorder()
    – Amit Bera
    Commented Oct 7, 2014 at 17:00
  • It's for images.php and I will give default position a shot thanks.
    – Francis
    Commented Oct 8, 2014 at 7:50

2 Answers 2

0

Try $_image->getPositionDefault()

print_r($_image->getData())

Array
(
    [value_id] =&gt; 2
    [file] => /l/a/label.jpg
    [label] => Label
    [position] => 0
    [disabled] => 0
    [label_default] => Label
    [position_default] => 0
    [disabled_default] => 0
    [url] => http://www.site.com/media/catalog/product/l/a/label.jpg
    [id] => 2
    [path] => /var/www/site/media/catalog/product/l/a/label.jpg
)
1
  • Thank you for that. That worked on a test image, but I accidently came across getPosition and it works wonders without me having to change much.
    – Francis
    Commented Oct 8, 2014 at 8:08
1

R.S' answer works but I came across a simpler way for me to do it. I simple changed.

if ($_image->getLabel() == 'hover'):

to

if ($_image->getPosition() == 2):

Thanks for your help folks, wouldn't of came across this otherwise. I know it looks stupid, but in a panic I missed the most obvious thing in the world and that was the fix.

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.