2

Following up to this question here I am wondering with my code I have right now

<?php
$_collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('product_url')
    ->addAttributeToSelect('product_reviews')
    ->joinAttribute('image', 'catalog_product/image', 'entity_id', null, 'left')
    ->addAttributeToFilter('entity_id', $this->getPid())
    ->setPageSize(1);

if ($_collection->getSize()) {
    $_product = $_collection->getFirstItem();
    $_productImage = Mage::helper('catalog/image')->init($_product, 'image');
    $_productName = $_product->getName();
    $_productReview = $_product->getReviewsSummaryHtml($_product, 'default', false);
    $_productPrice = $_product->getPrice();
    $_productUrl = $_product->getProductUrl();
}
?>

Then i thought i can iterate through the gallery images like this

<?php
$sliderImage = array();
if(count($_product->getMediaGalleryImages())) {
    foreach ($_product->getMediaGalleryImages() as $_image) {
        $bannerImage = $_image->getSlider();
        if ($bannerImage) {
            $sliderImage[]  = $_image;
        }
    }
}?>

I always get the base image of the product.

However I have around 5-8 images per product, and besides the standard ones, base, small and thumb i would like a different picture to be loaded here.

Is it possible to somehow get a different image? I have them all in a sort order so for example I would like the image with sort order 3.

7
  • 1
    What does $_product->getMediaGalleryImages() gives you ? Commented Jun 3, 2016 at 9:28
  • I have updated it, does not work, also I thought i can get the product reviews but that wont work either
    – seb
    Commented Jun 3, 2016 at 13:19
  • Or do I have to change the selection for the collection to get the Media Gallery Images?
    – seb
    Commented Jun 3, 2016 at 14:35
  • Yeah try that maybe Commented Jun 3, 2016 at 14:46
  • @Raphael at Digital Pianism, i tried that but dont really know how to change that join attribute. Refering to this link it seems like it is not possible so easily and I have to change the code in total.
    – seb
    Commented Jun 3, 2016 at 14:51

1 Answer 1

1

That is how i did it now

<?php
$_collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('product_url')
    ->joinAttribute('image', 'catalog_product/image', 'entity_id', null, 'left')
    ->addAttributeToFilter('entity_id', $this->getPid())
    ->setPageSize(1);

if ($_collection->getSize()) {
    $_product = $_collection->getFirstItem();
    $_productImage = Mage::helper('catalog/image')->init($_product, 'image');
    $_productName = $_product->getName();
    $_productPrice = $_product->getPrice();
    $_productUrl = $_product->getProductUrl();
}
?>

<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
                    ->setStoreId($storeId)
                    ->load($this->getPid());
?>
<?php 
$imagepath = array();
$productImage = Mage::getModel('catalog/product')->load($this->getPid());
foreach ($productImage->getMediaGalleryImages() as $image) {
                     $imagepath[] = $image->getUrl();
}  
?>

And then I can access all Images with $imagepath[X]

where X stands for the Sort order number.

Also with the SummaryData I can then access the Review Count and Rating.

So actually @Raphael at Digital Pianism was correct

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.