简体   繁体   中英

How to get Product Image in magento

I am using Magento community version 1.5.1.0.

I need to get a product have image or not. If a product have no image we want to assign its category image instead of magento default image.

How can I find that product have image or not?

Use the following code

<?php
        ini_set('display_errors','on');
        require_once 'app/Mage.php';
        Mage::app('default');
        $products = Mage::getModel('catalog/product')->load(1); //Product ID
        echo "<pre>";
        //print_r($products);
        echo $products->getImage();
        echo "<br>";
        echo $products->getSmallImage();
        echo "<br>";
        echo $products->getThumbnail();
        echo "<br>";
        echo Mage::helper('catalog/image')->init($products, 'small_image')->resize(163,100); // resize function is used to resize image
        echo "<br>";
        echo Mage::helper('catalog/image')->init($products, 'image')->resize(400,400);
    ?>
$product->getImage();

Use the above code, it gives the image if product have or it return the key " no_selection "

if($product->getImage() == 'no_selection')
{
     // PRODUCT HAVE NO IMAGE
}
else
{
     // PRODUCT HAVE IMAGE
}

With this we can findout that images was upload or not for a product.

尝试这个

if ($_product->getImage() != 'no_selection' && $_product->getImage()){put your category image retrive code }

When you call $your_product = Mage::getModel('catalog/product') in result you can find the media array details. So if you check for count of that array, you know the number of images associated to a product; if count is zero, no images are associated.

So:

if(count($your_product['media_gallery']['images']) > 0)
// PRODUCT HAVE IMAGE
else
// PRODUCT HAVE NO IMAGE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM