douhe6255
2013-12-22 19:20
浏览 54
已采纳

如何在Magento Api的产品列表方法结果中包含产品图像

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.

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;

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>

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?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douchi0028 2013-12-23 20:02
    最佳回答

    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;
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题