3

how to get all images by using below code. I'm only getting single image

<?php

class Affilnow_Module_Model_Product_Api extends Mage_Api_Model_Resource_Abstract
{
  public function items($attributes)
  {


    $catalog = Mage::getModel('catalog/product')
    ->getCollection()
    ->addStoreFilter(Mage::app()->getStore()->getId());

    $collection = $catalog->addAttributeToSelect("*");
    $tempres = array("helloworld");
    $tempres = json_encode($tempres);
    //var_dump($tempres);
    //echo "<xml>helloworld</xml>";
    //print_r($collection);
    $tempres = json_encode($collection);
    //return $tempres;
    //exit();
    foreach ($collection as $product) {

        $product_id = $product->getId();
        $assoc_variations = array();

        if ($product->isConfigurable()) {

            $assoc_products = $product->getTypeInstance(true)
            ->getConfigurableAttributesAsArray($product);

            foreach ($assoc_products as $assoc_product) {
                $assoc_variations[] = $assoc_product;
            }

        }


      $result[] = array(
        'id'          => $product->getId(),
        'name'        => $product->getName(), 
        'stock'       => $product->isInStock(), 
        'sku'         => $product->getSku(), 
        'weight'      => $product->getWeight(), 
        'description' => $product->getDescription(), 
        'image'       => $product->getImageUrl(), 
        'category'    => $product->getCategoryIds(), 
        'price'       => Mage::helper('core') -> currency($product -> getPrice(), true, false), 
        'hasOptions'  => $product->isConfigurable(), 
        'qty'         => intval($product->getQty()), 
        'created at'  => $product->getCreatedAt(),
        'variations'  => $assoc_variations
        'additional_images' =>$product->getMediaGalleryImage()
        );


  }

  return $result;
  }


}

2 Answers 2

3

Try 1:

$catalog = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*') 
->addStoreFilter(Mage::app()->getStore()->getId());

Try 2:

$backendModel = $catalog->getResource()->getAttribute('media_gallery')->getBackend();   

and foreach loop

foreach ($collection as $product) {
   $backendModel->afterLoad($product);
    foreach ($product->getMediaGalleryImages() as $image) {
       echo $image->getUrl();
    }    
}
2
  • and how to call them in result array ? Commented Feb 7, 2017 at 13:59
  • @rameez.hashmi you are always welcome Commented Feb 7, 2017 at 17:12
1

try this code :

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

foreach ($product->getMediaGalleryImages() as $image) {
    echo $image->getUrl();
}  
1
  • its not working . i'm getting null while using your code Commented Feb 7, 2017 at 12:23

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.