2013-12-22 23 views
1

我想最大限度地減少連接到基於Magento的商店以顯示產品的移動應用程序的Api調用數量。現在我們必須調用每個產品的catalog_product_attribute_media.list方法才能獲取圖片Url,並且它確實會降低應用的速度。如何在Magento Api的產品列表方法結果中包含產品圖像

我在answer中發現可以通過編輯某些腳本來擴展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 /應用/碼/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類型還是其他?

回答

1

感謝this answer我想出如何在結果中包括圖片: 這裏就是我修改了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; 
} 
+0

嗨,我添加了這段代碼,但是這不會返回我添加的任何附加參數。此外,我在wsdl文件中添加這些參數也清理了我的緩存,任何想法我想念。非常感謝。 –

0

轉到應用程序/代碼/核心/法師/目錄/型號/產品/ 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