如何包括在Magento阿比的产品列表法结果产品图片(How to include product

2019-10-19 03:22发布

我希望尽量减少API调用了它连接到基于Magento的店展示产品的移动应用程序的数量。 现在我们必须调用catalog_product_attribute_media.list为了获得图像URL方法为每个产品,它确实减慢了应用。

我在此找到了答案 ,这是可以通过编辑某些脚本,以扩展API调用的结果。 我试图用同样的方式通过编辑应用程序/代码/核心/法师/目录/型号/分类/ 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 /应用/代码/核心/法师/目录的/ 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类型还是其他什么东西?

Answer 1:

多亏了这个答案我想出如何在结果中包括图片:这里就是我修改了assignedProducts方法的应用程序/代码/核心/法师/目录/型号/分类/ Api.php和它的工作:

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


Answer 2:

转到应用程序/代码/核心/法师/目录/型号/产品/ 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



文章来源: How to include product images in Magento Api's product list method result