如何在 Magento Api 的产品列表方法结果中包含产品图片 [英] How to include product images in Magento Api's product list method result
问题描述
我想尽量减少连接到基于 Magento 的商店以展示产品的移动应用程序的 Api 调用次数.现在我们必须为每个产品调用 catalog_product_attribute_media.list
方法来获取图片 Urls,这确实减慢了应用程序的速度.
I want to minimize the number of Api calls for a mobile application which connects to Magento based shop to display products. right now we have to call catalog_product_attribute_media.list
method for each product in order to get image Urls and it really slows down the app.
我在此答案中发现可以通过编辑某些脚本来扩展 Api 调用的结果.我尝试使用相同的方法通过编辑 app/code/core/Mage/Catalog/Model/Category/Api.php 第 440 行将图像包含在产品列表中:
I found out in this answer that it is possible to extend the result of an Api Call by editing certain scripts. I tried to use the same approach to include images in product list by editing app/code/core/Mage/Catalog/Model/Category/Api.php line 440:
$storeId = $this->_getStoreId($store);
$collection = $category->setStoreId($storeId)->getProductCollection()
->addAttributeToSelect('brand')
->addAttributeToSelect('media_gallery_images');
($storeId == 0)? $collection->addOrder('position', 'asc') : $collection->setOrder('position', 'asc');;
$result = array();
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'),
'media' => $product->getMediaGalleryImages()
);
}
return $result;
我还编辑了 html/app/code/core/Mage/Catalog/etc/wsdl.xml 以包含新的媒体"属性行:255
I also edited html/app/code/core/Mage/Catalog/etc/wsdl.xml to include the new 'media' property line: 255
<complexType name="catalogAssignedProduct">
<all>
<element name="product_id" type="xsd:int"/>
<element name="type" type="xsd:string"/>
<element name="set" type="xsd:int"/>
<element name="sku" type="xsd:string"/>
<element name="position" type="xsd:int"/>
<element name="brand" type="xsd:string"/>
<element name="media" type="typens:catalogProductImageEntityArray"/>
</all>
</complexType>
但是当我调用 catalog_category.assignedProducts
时,它总是为 'media' 属性返回 null,我想知道为什么这不起作用?是xml类型还是别的什么?
but when I call the catalog_category.assignedProducts
it always returns null for the 'media' property, I wonder why this doesn't work? is it the xml type or something else?
推荐答案
感谢 这个答案 我想出了如何在结果中包含图像:这是我修改 app/code/core/Mage/Catalog/Model/Category/Api.php 中的assignedProducts 方法的方法,它起作用了:
Thanks to this answer I figured out how to include images in the results: here's how I modified the assignedProducts method in app/code/core/Mage/Catalog/Model/Category/Api.php and it worked:
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;
}
这篇关于如何在 Magento Api 的产品列表方法结果中包含产品图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!