如何获取特定产品的所有图像


8

如何通过使用此代码获取特定产品所有图像,我只能获取特定产品的单个图​​像。

请帮助我是新来的。

<?php 
// Get products in category
require_once('../app/Mage.php');//Path to Magento umask(0);

if(isset($_GET["categoryId"])){
  $categoryId=$_GET["categoryId"];
    $proxy = new SoapClient('http://www.prashant.com/index.php/api/v2_soap/?wsdl'); // TODO : change url
    $sessionId = $proxy->login('prash', 'prashant123@'); // TODO : change login and pwd if necessary

    $products= $proxy->catalogCategoryAssignedProducts($sessionId,$categoryId);


    $itemDetails=array();
    Mage::app(); 

  foreach ($products as $item) { 

    $product2 = Mage::getModel('catalog/product')->load($item->product_id);
    $productMediaConfig = Mage::getModel('catalog/product_media_config');
    $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
    $price2=0;
    $price2=$product2->getFinalPrice();
    if($price2==0){
    $price2=$product2->getPrice();
    }



    $desc=$product2->getDescription();
           if($desc==""){
            $desc="blank";
           }
           $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
           if($baseImageUrl=="http://www.prashant.com/media/catalog/product/"){
            $baseImageUrl="http://www.prashant.com/customApi/icon.png";
           }

           //"desc"=>$desc,

           $statusp=$product2->getStatus();
    $statusen= Mage_Catalog_Model_Product_Status::STATUS_ENABLED;


    if($statusp==$statusen){
         $itemDetails[]=array(
      "productId"=>$item->product_id,
      "name"=>$product2->getName(),
      "price"=>$product2->getPrice(),
      "spprice"=>$price2,
      "desc"=>$desc,
      "imageurl"=>$baseImageUrl,
      );
    }


  }
  //return array of products
  //echo "<pre>";
  //print_r($itemDetails);
  //echo "</pre>";
  echo json_encode($itemDetails);
  exit();
}else{
  echo "error";
  exit();
}
?>

Answers:


8

使用以下代码可利用特定产品的所有图像。

$product = Mage::getModel('catalog/product')->load(2);
$base = $product->getImage();
$thumb = $product->getThumbnail();
$small = $product->getSmallImage();

foreach ($product->getMediaGalleryImages() as $image) 
{ //will load all gallery images in loop
    //print_r($image);
      $file_name = $image->getFile();
}

4

您可以通过以下脚本获得产品的所有图像,包括被排除的图像:

$product->getMediaGalleryImages('images')

$product should be an instance of Mage_Catalog_Model_Product

3

在“变量”下方,将返回与该产品ID相关的所有媒体图像的数组。

 $images = Mage::getModel('catalog/product')->load($item->product_id)->getMediaGalleryImages();

2
$result = $client->catalogProductAttributeMediaList($session, $_productId);
echo "<pre>"; print_r($result);

2
<?php foreach ($product->getMediaGalleryImages() as $image) :?>

<img src="<?php echo Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(100, 100); ?>" alt="<?php echo $product->getName()?>" />

<?php endforeach; ?>

1

getImage()函数只能为您提供产品的基本图像

如果要获取产品的所有图像,则应致电媒体画廊

如果完整的产品对象,则可以使用 以下代码来媒体图像

$mediaGallery = $product->getMediaGalleryImages();
    /* get Image one by  using loop*/
    foreach ($product2->getMediaGalleryImages() as $image) {
        echo ($image->getUrl());
    $productMediaConfig->getMediaUrl($image->getUrl());
    } 

之后 $desc=$product2->getDescription();

请注意:如您所愿full load the product model by product2 = Mage::getModel('catalog/product')->load($item->product_id);

然后,您可以getMediaGalleryImages() 轻松使用媒体图像。

如果您有收藏,则使用 更快的方式将媒体图片加载到产品收藏中以获取媒体图片


1

试试这个:

$product = Mage::getModel('catalog/product')->load(3);//product id 3

    foreach ($product->getMediaGalleryImages() as $image) {
                         echo $image->getUrl();
    }  
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.