如何编辑Magento API函数“items()”以获取产品的Image Url

如何编辑Magento API函数“items()”以获取产品的Image Url

问题描述:

I am trying to get the Image URL of the products by editing the Magento's API items() function. I have used $product-> getImageUrl() to get the URL but I am getting wrong URL.

The URL that I am getting is of the default Image which we place for the products which do not have image(Image Comming Soon like Image's url).

I am calling the function from Android Client using XML-RPC.
I am getting other details of the product correct,but the URL that I am getting for the products is wrong. And, all the URLs of the different products I am getting are same.

FYI, The URL that I am getting in the response is like :

http://192.168.1.237/machinetest/media/catalog/product/cache/0/image/265x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg

The function that I am editing is as followed :

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
             'url_path'  =>  $product-> getImageUrl() // Added the Method here 
        );
    }
    return $result;
}

Just write that please try this way..you will get the Solution on the Top

You can able to get Images using this code just go through it and you will get images

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }

                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }

    $result = array();

    foreach ($collection as $product) {
//            $result[] = $product->getData();

        $_product = Mage::getModel('catalog/product')->load($product->getId());
        $_image=$_product->getImageUrl();

        $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),

            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
            'image_url_path' =>  $_image
        );
    }

    return $result;
}

hope it's work if you have any queries tell me i will help you!

Your code was awesome!

Yes, you can get image url with :

'url_path'  =>  Mage::getModel('catalog/product_media_config')
                ->getMediaUrl($product->getImage());//getSmallImage(), getThumbnail()

or another option by calling :

$type = 'small_image';
'url_path'  => $this->helper('catalog/image')
                   ->init($product, $type)
                   ->resize(163, 100);

can be changed by 'image' small_image' or 'thumbnail'

Default:

  • Base Image: 265x265 pixel

  • Small Image: 135x135 pixel

  • Thumbnail Image: 75x75 pixel


The easier option (detailed):

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $full_product = Mage::getModel('catalog/product')->load($product_id);

           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids'=> $product->getCategoryIds(),
            'url_path'  =>  $full_product->getImageUrl(),
            // 'product_path' => $full_product->getProductUrl() 
            // you can call $full_product->getData();
        );
    }
    return $result;
}

I was working with images recently and I believe I have pretty good understanding about it.

I don't think what Josua said is the "real" correct answer. It is good that his answer can solve your problem but I just couldn't stand seeing misleading information.

As for his first option, it is "correct".

Let me break down the code:

Mage_Catalog_Model_Product_Media_Config

public function getMediaUrl($file)
{
    $file = $this->_prepareFileForUrl($file);

    if(substr($file, 0, 1) == '/') {
        return $this->getBaseMediaUrl() . $file;
    }

    return $this->getBaseMediaUrl() . '/' . $file;
}

public function getBaseMediaUrl()
{
    return Mage::getBaseUrl('media') . 'catalog/product';
}

protected function _prepareFileForUrl($file)
{
    return str_replace(DS, '/', $file);
}

As you can see, it just simply add media/ + catalog/product + $file.

$file is taken from database, the value will be something like /e/x/example.jpeg

Your uploaded product images are stored inside those folders.

Now, for the problem why $product-> getImageUrl() give you wrong URL is still unknown.

The code that Josua suggest for second option:

$this->helper('catalog/image')
    ->init($product, $type)
    ->resize(163, 100);

is "almost" the same with $product->getImageUrl(), it just have difference in resize

Mage_Catalog_Model_Product

public function getImageUrl()
{
    return (string)$this->_getImageHelper()->init($this, 'image')->resize(265);
}

So for his second option, it will give the same result with your old code. I don't know why did he suggest the second option, I think he never check what is behind those functions (not a good idea as it can lead to wrong information)

When you call for $product->getImageUrl(), it will try to load your image from cache if it exists, if not, it will load the image from database (for the path and then will look for your correct image from media folder) and create the cache. If it is unable to find the image or an error occurred, it will get the placeholder image.

My suggestion is to check if there is an exception thrown. You need to use your old code $product->getImageUrl(). Open your app/code/core/Mage/Catalog/Helper/Image.php

Then go to:

Mage_Catalog_Helper_Image

public function __toString()
{
    try {
        if( $this->getImageFile() ) {
            $this->_getModel()->setBaseFile( $this->getImageFile() );
        } else {
            $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
        }

        if( $this->_getModel()->isCached() ) {
            return $this->_getModel()->getUrl();
        } else {
            if( $this->_scheduleRotate ) {
                $this->_getModel()->rotate( $this->getAngle() );
            }

            if ($this->_scheduleResize) {
                $this->_getModel()->resize();
            }

            if( $this->getWatermark() ) {
                $this->_getModel()->setWatermark($this->getWatermark());
            }

            $url = $this->_getModel()->saveFile()->getUrl();
        }
    } catch( Exception $e ) {
        //put log to show error message
        Mage::log($e->getMessage());
        $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
    }
    return $url;
}

Put Mage::log($e->getMessage()); to log if there is an exception thrown. Most likely your placeholder image is called because there was an exception thrown.

It is just a suggestion from me to ensure there is nothing's wrong with your image / other things as in fact you have solved your problem by directly get the image from media/catalog/product/...

Another correction for Josua's code:

Notice the $full_product = Mage::getModel('catalog/product')->load($product_id);

It is absolutely unnecessary since inside foreach($collection as $product), the product object will be loaded, so another load of product is unnecessary (also $product_id is undefined)

UPDATE, just fixing your code:

public function items($filters = null, $store = null)
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect(array('name','image'));
        //->addAttributeToSelect('name'); add another select, either image / small_image / thumbnail, modify it as you need

    if (is_array($filters)) {
        try {
            foreach ($filters as $field => $value) {
                if (isset($this->_filtersMap[$field])) {
                    $field = $this->_filtersMap[$field];
                }
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
    }
    $result = array();

    foreach ($collection as $product) {
         //$result[] = $product->getData();
           $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
           'category_ids'=> $product->getCategoryIds(),
             'url_path'  =>  $product-> getImageUrl() // Added the Method here 
        );
    }
    return $result;
}