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

时间:2013-12-22 19:20:55

标签: php api magento soap-client

我希望尽量减少移动应用程序的Api调用次数,该应用程序连接到基于Magento的商店以显示产品。现在我们必须为每个产品调用catalog_product_attribute_media.list方法才能获取图片网址,这确实会降低应用的速度。

我在answer中发现可以通过编辑某些脚本来扩展Api调用的结果。我尝试使用相同的方法通过编辑app / code / core / Mage / Catalog / Model / Category / Api.php第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以包含新的'media'属性行: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类型还是别的什么?

2 个答案:

答案 0 :(得分:1)

感谢this answer我想出了如何在结果中包含图片: 这是我在app / code / core / Mage / Catalog / Model / Category / Api.php中修改assignedProducts方法的方法,它起作用了:

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 :(得分:0)

转到app / code / core / Mage / Catalog / Model / Product / Api.php并替换以下函数

    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;
    }

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