3

I need to get a comma separated list of image URL's from a product's media gallery.

I have successfully managed to load getMediaGalleryImages so that if I...

var_dump ($product->getMediaGalleryImages()); 

... I can see each image being loaded with it's attributes, i.e:-

["url"]=> string(89) "https://www.example.com/media/catalog/product/x/x/x-image.jpg" 

I just haven't managed to figure out how to list them in an array like:-

https://www.example.com/media/catalog/product/x/x/x-image.jpg, https://www.example.com/media/catalog/product/x/x/x-image_1.jpg, https://www.example.com/media/catalog/product/x/x/x-image_2.jpg 

Struggling to get the syntax right I think...

2 Answers 2

6

I figured this out earlier so here is how I displayed a comma separated list of image URL's from getMediaGalleryImages...

$gallery_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); $items = array() foreach($gallery_images as $g_image) { $items[] = $g_image['url']; } 

Then I could output with:-

implode(', ', $items); 
1
  • 2
    Avoid loading the entire object, it's better to add the gallery images to your collection. Commented Dec 9, 2016 at 15:09
0
<?php error_reporting(E_ALL | E_STRICT); define('MAGENTO_ROOT', getcwd()); $mageFilename = MAGENTO_ROOT . '/app/Mage.php'; require_once $mageFilename; Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); Mage::app(); $file = fopen('getId.csv', 'r'); $header = fgetcsv($file); $headers[] = array("id","sku","images"); while ( $row = fgetcsv($file, 6000, ",") ) { $data = array(); $data = array_combine($header, $row); $id = $data['id']; $product = Mage::getModel('catalog/product')->load($id); $getId = $product->getId(); $sku = $product->getSku(); $gallery_images = $product->getMediaGalleryImages(); $items = array(); foreach($gallery_images as $g_image) { $items[] = $g_image->getUrl(); } $headers[]=array($getId,$sku,implode(',',$items)); } $fp = fopen("getImagePath.csv","w"); foreach ($headers as $line) { fputcsv($fp, $line); } fclose($fp); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.