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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料