【问题标题】:How to include product images in Magento's products list Rest Api如何在 Magento 的产品列表中包含产品图片 Rest Api
【发布时间】:2015-05-27 04:36:15
【问题描述】:

我是 magento 的新手,并且正在制作它的移动应用程序,因为我正在使用 REST API。但是在产品列表 API 中,我想添加至少一个产品图片,这样应用就不会花费太多时间来加载。

我看到一个类似的question 对于提供以下解决方案的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 的信息。我试图在app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php 类的protected function _retrieveCollection() 中做某事,但没有成功。

【问题讨论】:

    标签: php api rest magento magento-1.9


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

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

    正确方法:
    为 API 创建一个自定义扩展,并在扩展中添加上面的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多