简体   繁体   English

如何在Magento的产品列表中包含产品图像Rest Api

[英]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 . 我是magento的新手,并且正在对其进行移动应用程序,因为我正在使用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. 但是在“产品列表API”中,我想至少添加一个产品图片,这样应用程序就不需要花费很多时间来加载。

I saw a similar question for SOAP API that provided below solution: 我对SOAP API看到了类似的问题 ,该问题提供了以下解决方案:

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 . 但是我找不到关于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. 我试图在类app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php protected function _retrieveCollection()中执行某些操作,但未成功。

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 . 如果您正在使用管理上下文来获取产品集合,则响应中将使用Mage_Catalog_Model_Api2_Product_Rest_Admin_V1 :: _ retrieveCollection来获取图像URL链接。

If you are using the customer or guest context you will get 'image_url' for each product which is the standard size image. 如果您使用的是客户或来宾上下文,则将为每个产品获得“ image_url”,这是标准尺寸的图像。 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. 通过调用集合中每个产品的Mage_Catalog_Model_Api2_Product_Rest :: _ prepareProductForResponseMage_Catalog_Model_Api2_Product_Rest :: _ retrieveCollection中进行处理。 You could extend Mage_Catalog_Model_Api2_Product_Rest::_prepareProductForResponse and add additional image URLs for 'small_image' and 'thumbnail' if you wanted. 您可以扩展Mage_Catalog_Model_Api2_Product_Rest :: __ prepareProductForResponse并为“ small_image”和“ thumbnail”添加其他图像URL。

Quick Fix: 快速解决:
In: app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php 在:app / code / core / Mage / Catalog / Model / Api2 / Product / Rest.php
Under the function : _prepareProductForResponse function 在函数下:_prepareProductForResponse函数
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. 为API创建一个自定义扩展,然后在扩展中添加以上行。

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

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