简体   繁体   English

如何在magento中获得产品形象

[英]How to get Product Image in magento

I am using Magento community version 1.5.1.0. 我正在使用Magento社区版本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. 如果产品没有图像,我们要分配其类别图像,而不是magento默认图像。

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 " 使用上面的代码,如果产品具有图像或返回键“ 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. 结果是,当您调用$your_product = Mage::getModel('catalog/product') ,您可以找到介质阵列的详细信息。 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. 如果count为零,则不关联任何图像。

So: 所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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