0

i'm trying to get main image (original image) at magento 1.4 (the flat product is on) using this script

<?php 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('memory_limit', '8192M');


require_once('app/Mage.php');

$app = Mage::app();

$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*');

$i = 0;
foreach ($collection as $product) {

    echo $i.'-----Main image'.$product->getImage().'<br>';
    $i++;

    if ($i == 50) {
        break;
    }

}


?>

but it return nothing/empty, the output is here,

0-----Main image
1-----Main image
2-----Main image
3-----Main image
4-----Main image
5-----Main image
6-----Main image
7-----Main image
8-----Main image
9-----Main image
10-----Main image
11-----Main image
12-----Main image
13-----Main image
14-----Main image
15-----Main image
16-----Main image
17-----Main image
18-----Main image
19-----Main image
20-----Main image
21-----Main image
22-----Main image
23-----Main image
24-----Main image
25-----Main image
26-----Main image
27-----Main image
28-----Main image
29-----Main image
30-----Main image
31-----Main image
32-----Main image
33-----Main image
34-----Main image
35-----Main image
36-----Main image
37-----Main image
38-----Main image
39-----Main image
40-----Main image
41-----Main image
42-----Main image
43-----Main image
44-----Main image
45-----Main image
46-----Main image
47-----Main image
48-----Main image
49-----Main image

anyone know what are the problems?

3

What I normally do to get the product images, is using getMediaGalleryImages. This means that your code would look something like this:

<?php
require_once('app/Mage.php');
umask(0);

Mage::app();

/** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*');

$i = 0;

/** @var Mage_Catalog_Model_Product $product */
foreach ($collection as $product) {
    // 1) Get all the product images
    $images = $product->getMediaGalleryImages();

    foreach ($images as $image) {
        echo $i . "-----Image: '.$image->getValue().'<br>';
    }

    // 2) Get the main image
    echo $i . "-----Main Image: '. Mage::helper('catalog/image')->init($product, 'image') .'<br>';

    $i++;

    if ($i == 50) {
        break;
    }
}

As you can see by my comments, I added two ways to get product images. The first option gets all product images and only the path, not the entire URL.

The second option will get the entire URL of the main product image.

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.