问题
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?
回答1:
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;
}
回答2:
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
来源:https://stackoverflow.com/questions/20733087/how-to-include-product-images-in-magento-apis-product-list-method-result