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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglu2523 2016-08-29 06:51

    Go to app/code/core/Mage/Catalog/Model/Product/Api.php and replace the below function

        public function items($filters = null, $store = null)
        {
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addStoreFilter($this->_getStoreId($store))
                ->addAttributeToSelect('name');
    
            /** @var $apiHelper Mage_Api_Helper_Data */
            $apiHelper = Mage::helper('api');
            $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
            try {
                foreach ($filters as $field => $value) {
                    $collection->addFieldToFilter($field, $value);
                }
            } catch (Mage_Core_Exception $e) {
                $this->_fault('filters_invalid', $e->getMessage());
            }
            $result = array();
            foreach ($collection as $product) {
    
       $_product = Mage::getModel('catalog/product')->load($product->getId());
       $_image = $_product->getImageUrl();
                $result[] = array(
                    'product_id' => $product->getId(),
                    'sku'        => $product->getSku(),
                    'name'       => $product->getName(),
                    'set'        => $product->getAttributeSetId(),
                    'type'       => $product->getTypeId(),
                    'category_ids' => $product->getCategoryIds(),
        'image_url'  => $_image,
                    'website_ids'  => $product->getWebsiteIds()
                );
            }
            return $result;
        }
    

    Code: http://chandreshrana.blogspot.in/2015/05/add-image-in-product-list-api-magento.html

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题