8

How to get all images of a particular product by using this code i am able to fetch only single image of the particular product..

Please help i am new to this.

<?php 
// Get products in category
require_once('../app/Mage.php');//Path to Magento umask(0);

if(isset($_GET["categoryId"])){
  $categoryId=$_GET["categoryId"];
    $proxy = new SoapClient('http://www.prashant.com/index.php/api/v2_soap/?wsdl'); // TODO : change url
    $sessionId = $proxy->login('prash', 'prashant123@'); // TODO : change login and pwd if necessary

    $products= $proxy->catalogCategoryAssignedProducts($sessionId,$categoryId);


    $itemDetails=array();
    Mage::app(); 

  foreach ($products as $item) { 

    $product2 = Mage::getModel('catalog/product')->load($item->product_id);
    $productMediaConfig = Mage::getModel('catalog/product_media_config');
    $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
    $price2=0;
    $price2=$product2->getFinalPrice();
    if($price2==0){
    $price2=$product2->getPrice();
    }



    $desc=$product2->getDescription();
           if($desc==""){
            $desc="blank";
           }
           $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
           if($baseImageUrl=="http://www.prashant.com/media/catalog/product/"){
            $baseImageUrl="http://www.prashant.com/customApi/icon.png";
           }

           //"desc"=>$desc,

           $statusp=$product2->getStatus();
    $statusen= Mage_Catalog_Model_Product_Status::STATUS_ENABLED;


    if($statusp==$statusen){
         $itemDetails[]=array(
      "productId"=>$item->product_id,
      "name"=>$product2->getName(),
      "price"=>$product2->getPrice(),
      "spprice"=>$price2,
      "desc"=>$desc,
      "imageurl"=>$baseImageUrl,
      );
    }


  }
  //return array of products
  //echo "<pre>";
  //print_r($itemDetails);
  //echo "</pre>";
  echo json_encode($itemDetails);
  exit();
}else{
  echo "error";
  exit();
}
?>

7 Answers 7

8

Use the following code to avail all the images of a particular product.

$product = Mage::getModel('catalog/product')->load(2);
$base = $product->getImage();
$thumb = $product->getThumbnail();
$small = $product->getSmallImage();

foreach ($product->getMediaGalleryImages() as $image) 
{ //will load all gallery images in loop
    //print_r($image);
      $file_name = $image->getFile();
}
4

Below Variable returns you array of all media images with respect to that product id.

 $images = Mage::getModel('catalog/product')->load($item->product_id)->getMediaGalleryImages();
4

You can get products all images by following script including images that are excluded:

$product->getMediaGalleryImages('images')

$product should be an instance of Mage_Catalog_Model_Product
2
$result = $client->catalogProductAttributeMediaList($session, $_productId);
echo "<pre>"; print_r($result);
2
<?php foreach ($product->getMediaGalleryImages() as $image) :?>

<img src="<?php echo Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(100, 100); ?>" alt="<?php echo $product->getName()?>" />

<?php endforeach; ?>
1

getImage() function only able to give you base image of a product.

If want to get all images of product then you should to call media gallery.

If you full product object then you can media images using below code:

$mediaGallery = $product->getMediaGalleryImages();
    /* get Image one by  using loop*/
    foreach ($product2->getMediaGalleryImages() as $image) {
        echo ($image->getUrl());
    $productMediaConfig->getMediaUrl($image->getUrl());
    } 

after $desc=$product2->getDescription();.

Note that: As you have full load the product model by product2 = Mage::getModel('catalog/product')->load($item->product_id);

Then you media image using getMediaGalleryImages() easily.

If you have collection then use Faster way to load media images in a product collection for getting media image

1

Try this one:

$product = Mage::getModel('catalog/product')->load(3);//product id 3

    foreach ($product->getMediaGalleryImages() as $image) {
                         echo $image->getUrl();
    }  

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.