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;
  }


}
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 ? Feb 7 '17 at 13:59
  • @rameez.hashmi you are always welcome Feb 7 '17 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 Feb 7 '17 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.