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

问题描述 投票:0回答:2

我是 magento 的新手,也不太熟悉它的移动应用程序,因为我正在使用

REST API
。但在产品列表 API 中,我想至少添加产品的单个图像,以便应用程序不会花费太多时间来加载。

我看到了类似的问题 对于

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;
}

但是我找不到任何关于

REST API
的信息。我尝试在
protected function _retrieveCollection()
班级
app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php
做一些事情,但没有成功。

php magento magento-1.9 magento-rest-api
2个回答
0
投票

如果您使用管理上下文来获取产品集合,您将不会在响应中获得图像 URL 链接,因为它使用 Mage_Catalog_Model_Api2_Product_Rest_Admin_V1::_retrieveCollection

如果您使用客户或访客上下文,您将获得每个产品的“image_url”,这是标准尺寸的图像。这是通过为集合中的每个产品调用 Mage_Catalog_Model_Api2_Product_Rest::_prepareProductForResponseMage_Catalog_Model_Api2_Product_Rest::_retrieveCollection 中处理的。如果需要,您可以扩展 Mage_Catalog_Model_Api2_Product_Rest::_prepareProductForResponse 并为“small_image”和“thumbnail”添加其他图像 URL。


0
投票

快速修复:
位于:app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php
函数下:_prepareProductForResponse函数
添加以下行以显示图像。

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

正确做法:
创建 API 的自定义扩展并在扩展中添加上面的行。

© www.soinside.com 2019 - 2024. All rights reserved.