How to get a product's image in Magento?

后端 未结 12 1110
慢半拍i
慢半拍i 2020-12-01 06:32

I\'m running on version 1.3.2.1, but on my client\'s server they had Magento 1.3.0 so my previous code to display images for my local copy,

echo $this->he         


        
相关标签:
12条回答
  • 2020-12-01 06:51

    Here is the way I've found to load all image data for all products in a collection. I am not sure at the moment why its needed to switch from Mage::getModel to Mage::helper and reload the product, but it must be done. I've reverse engineered this code from the magento image soap api, so I'm pretty sure its correct.

    I have it set to load products with a vendor code equal to '39' but you could change that to any attribute, or just load all the products, or load whatever collection you want (including the collections in the phtml files showing products currently on the screen!)

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addFieldToFilter(array(
        array('attribute'=>'vendor_code','eq'=>'39'),
    ));
    
    $collection->addAttributeToSelect('*');
    
    foreach ($collection as $product) {
    
        $prod = Mage::helper('catalog/product')->getProduct($product->getId(), null, null);
    
        $attributes = $prod->getTypeInstance(true)->getSetAttributes($prod);
    
        $galleryData = $prod->getData('media_gallery');
    
        foreach ($galleryData['images'] as &$image) {
            var_dump($image);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 06:52

    get product images in magento using product id

        $product_id = $_POST['product_id'];
        $storeId    = Mage::app()->getStore()->getId();
        $loadpro    = Mage::getModel('catalog/product')->load($product_id);
    
        $mediaApi      =  Mage::getModel("catalog/product_attribute_media_api");
        $mediaApiItems = $mediaApi->items($loadpro->getId());
        foreach ($mediaApiItems as $item) {
                //for getting existing Images
                echo $item['file'];
                }
    
    0 讨论(0)
  • 2020-12-01 06:56

    You can try to replace $this-> by Mage:: in some cases. You need to convert to string.

    In my case i'm using DirectResize extension (direct link), so my code is like this:

    (string)Mage::helper('catalog/image')->init($_product, 'image')->directResize(150,150,3)
    

    The ratio options (3rd param) are :

    • none proportional. The image will be resized at the Width and Height values.
    • proportional, based on the Width value 2
    • proportional, based on the Height value 3
    • proportional for the new image can fit in the Width and the Height values. 4
    • proportional. The new image will cover an area with the Width and the Height values.

    Update: other info and versions here


    The common way, without plugin would be:

    (string)Mage::helper('catalog/image')->init($_product, 'image')->resize(150)
    

    You can replace 'image' with 'small_image' or 'thumbnail'.

    0 讨论(0)
  • 2020-12-01 06:57
    // Let's load the category Model and grab the product collection of that category
    
    $product_collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
    
    // Now let's loop through the product collection and print the ID of every product 
    foreach($product_collection as $product) {
      // Get the product ID
    
    $product_id = $product->getId();
    
      // Load the full product model based on the product ID
    
    $full_product = Mage::getModel('catalog/product')->load($product_id);
    
      // Now that we loaded the full product model, let's access all of it's data
    
      // Let's get the Product Name
    
      $product_name = $full_product->getName();
    
      // Let's get the Product URL path
    
      $product_url = $full_product->getProductUrl();
    
      // Let's get the Product Image URL
    
      $product_image_url = $full_product->getImageUrl();
    
      // Let's print the product information we gathered and continue onto the next one
    
     echo $product_name;
    
      echo $product_image_url;
    
    
    }
    
    0 讨论(0)
  • 2020-12-01 07:00
    $model = Mage::getModel('catalog/product'); //getting product model
    $_products = $model->getCollection(); //getting product object for particular product id
    foreach($_products as $_product) { ?>
        <a href = '<?php echo $model->load($_product->getData("entity_id"))->getUrl_path(); ?>'> <img src= '<?php echo $model->load($_product->getData("entity_id"))->getImageUrl();  ?>' width="75px" height="75px"/></a>
         <?php echo "<br/>".$model->load($_product->getData("entity_id"))->getPrice()."<br/>". $model->load($_product->getData("entity_id"))->getSpecial_price()."<br/>".$model->load($_product->getData("entity_id"))->getName()?>
    <?php 
    
    0 讨论(0)
  • 2020-12-01 07:02

    You need set image type :small_image or image

    echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(163, 100);
    
    0 讨论(0)
提交回复
热议问题