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

时间:2015-05-27 04:36:16

标签: php api rest magento magento-1.9

我是magento的新手并正在使用它的移动应用程序,因为我正在使用SOAP API。但是在产品列表API中,我想添加至少单个产品图像,以便应用程序不会花费太多时间来加载。

我看到了类似的question 对于提供以下解决方案的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 {{1}}中做一些事情,但没有成功。

2 个答案:

答案 0 :(得分:0)

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

如果您使用的是客户或访客上下文,您将获得每个产品的'image_url',这是标准尺寸的图像。通过对集合中的每个产品调用 Mage_Catalog_Model_Api2_Product_Rest :: _ prepareProductForResponse ,在 Mage_Catalog_Model_Api2_Product_Rest :: _ retrieveCollection 中处理此问题。您可以扩展 Mage_Catalog_Model_Api2_Product_Rest :: _ prepareProductForResponse ,并根据需要为'small_image'和'thumbnail'添加其他图片网址。

答案 1 :(得分:0)

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

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

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

相关问题