简体   繁体   中英

How to include product images in Magento's products list Rest Api

I am new to magento and making mobile application of it, for that I am using REST API . But in Product list API I want to add at least single image of product so that app will not take much time to load.

I saw a similar question for SOAP API that provided below solution:

public function assignedProducts($categoryId, $store = null)
{
    $category = $this->_initCategory($categoryId);

    $storeId = $this->_getStoreId($store);
    $collection = $category->setStoreId($storeId)->getProductCollection()
    ->addAttributeToSelect(array('brand','image','price','description','short_description','name'));
    ($storeId == 0)? $collection->addOrder('position', 'asc') : $collection->setOrder('position', 'asc');

    $result = array();
    $type = 'image';
    foreach ($collection as $product) {
        $result[] = array(
            'product_id' => $product->getId(),
            'type'       => $product->getTypeId(),
            'set'        => $product->getAttributeSetId(),
            'sku'        => $product->getSku(),
            'position'   => $product->getCatIndexPosition(),
            'brand'      => $product->getData('brand'),
            'price'      => $product->getData('price'),
            'name'      => $product->getData('name'),
            'description'      => $product->getData('description'),
            'short_description'      => $product->getData('short_description'),
            'image_url'  => $product-> getImageUrl() 
        );
    }

    return $result;
}

But I am unable to find anything about REST API . I tried to do something in protected function _retrieveCollection() of class app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php but no success.

If you are using the admin context to get the product collection you will not get image URL links in the response since it uses Mage_Catalog_Model_Api2_Product_Rest_Admin_V1::_retrieveCollection .

If you are using the customer or guest context you will get 'image_url' for each product which is the standard size image. This is handled in Mage_Catalog_Model_Api2_Product_Rest::_retrieveCollection via a call to Mage_Catalog_Model_Api2_Product_Rest::_prepareProductForResponse for each product in the collection. You could extend Mage_Catalog_Model_Api2_Product_Rest::_prepareProductForResponse and add additional image URLs for 'small_image' and 'thumbnail' if you wanted.

Quick Fix:
In: app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php
Under the function : _prepareProductForResponse function
Add the following line to show the image.

 $productData['image_url'] = (string) Mage::helper('catalog/image')->init($product, 'image');

Proper way:
Create an custom extension to the API and add the line above in the extension.

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