【问题标题】:How to include product images in Magento Api's product list method result如何在 Magento Api 产品列表方法结果中包含产品图像
【发布时间】:2013-12-22 19:20:54
【问题描述】:

我想尽量减少连接到基于 Magento 的商店以展示产品的移动应用程序的 Api 调用次数。现在我们必须为每个产品调用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 以包含新的“媒体”属性行: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 时,它总是为“媒体”属性返回 null,我想知道为什么这不起作用?是xml类型还是别的?

【问题讨论】:

    标签: php api magento soap-client


    【解决方案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;
    }
    

    【讨论】:

    • 嗨,我添加了这段代码,但这不会返回我添加的任何其他参数。此外,我在 wsdl 文件中添加这些参数也清理了我的缓存,知道我缺少什么。非常感谢您的帮助。
    【解决方案2】:

    去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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      相关资源
      最近更新 更多