0

From a product I want to get all the images it has in the gallery and sort them so that the main/base image of the product is first in the list. Firstly, I have a problem because I can't get the main/basic image from the list and secondly, how can I rearrange the rest of the images.

I tried to get the main/basic image in the following way:

foreach ($data["media_gallery"]["images"] as $image) {
    if ($image['file'] === $this->product->getImage()) {
        //
    } else {
        //
    }
}

The problem is that if you have several images and you don't have the main/base image marked as the first in the list, according to the code mentioned above, it doesn't return the right image, but always returns the first one in the list as the header/base.

Can anybody help me how to first get image and then how to properly sort them.

Thank you

1 Answer 1

0

Assumed you wrote a custom code in your template to get the product gallery images.

You should use the following method getMediaGalleryImages, which returns the product gallery images Collection in Order (Main / Base Image First followed by the other product images)

/* var Mage_Catalog_Model_Product $_product */

$mediaImages  = $_product->getMediaGalleryImages();

foreach ($mediaImages as $_mediaImage) {
    echo '<br/>'.Mage::helper('catalog/image')->init($_product, 'image', $_mediaImage->getFile());
}

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.