2012-08-31 49 views
2

我試圖通過編輯Magento的APIitems() function得到圖片網址產品的的的ImageUrl。我用$product-> getImageUrl()得到了的URL但我錯了URL如何編輯Magento的API函數「項目()」來獲得產品

URL,我得到的是默認圖像我們將其放在了產品不具有圖像(圖像即將推出類似圖片的URL)的。

我從撥打該功能Android客戶端使用XML-RPC
我收到了正確的產品的其他詳細信息,但我得到的產品的URL是錯誤的。而且,我所得到的不同產品的所有網址都是一樣的。

僅供參考,我在響應我得到的URL是這樣的:

http://192.168.1.237/machinetest/media/catalog/product/cache/0/image/265x/9df78eab33525d08d6e5fb8d27136e95/images /catalog/product/placeholder/image.jpg

,我編輯的功能如下:

public function items($filters = null, $store = null) 
{ 
    $collection = Mage::getModel('catalog/product')->getCollection() 
     ->addStoreFilter($this->_getStoreId($store)) 
     ->addAttributeToSelect('name'); 

    if (is_array($filters)) { 
     try { 
      foreach ($filters as $field => $value) { 
       if (isset($this->_filtersMap[$field])) { 
        $field = $this->_filtersMap[$field]; 
       } 
       $collection->addFieldToFilter($field, $value); 
      } 
     } catch (Mage_Core_Exception $e) { 
      $this->_fault('filters_invalid', $e->getMessage()); 
     } 
    } 
    $result = array(); 

    foreach ($collection as $product) { 
     //$result[] = $product->getData(); 
      $result[] = array(// Basic product data 
      'product_id' => $product->getId(), 
      'sku'  => $product->getSku(), 
      'name'  => $product->getName(), 
      'set'  => $product->getAttributeSetId(), 
      'type'  => $product->getTypeId(), 
      'category_ids'=> $product->getCategoryIds(), 
      'url_path' => $product-> getImageUrl() // Added the Method here 
     ); 
    } 
    return $result; 
} 

回答

4

只寫請儘量將way..you將獲得頂級

的解決方案可以能夠得到使用該代碼的圖片只是通過它,您將獲得的圖像

public function items($filters = null, $store = null) 
{ 
    $collection = Mage::getModel('catalog/product')->getCollection() 
     ->addStoreFilter($this->_getStoreId($store)) 
     ->addAttributeToSelect('name'); 

    if (is_array($filters)) { 
     try { 
      foreach ($filters as $field => $value) { 
       if (isset($this->_filtersMap[$field])) { 
        $field = $this->_filtersMap[$field]; 
       } 

       $collection->addFieldToFilter($field, $value); 
      } 
     } catch (Mage_Core_Exception $e) { 
      $this->_fault('filters_invalid', $e->getMessage()); 
     } 
    } 

    $result = array(); 

    foreach ($collection as $product) { 
//   $result[] = $product->getData(); 

     $_product = Mage::getModel('catalog/product')->load($product->getId()); 
     $_image=$_product->getImageUrl(); 

     $result[] = array(// Basic product data 
      'product_id' => $product->getId(), 
      'sku'  => $product->getSku(), 

      'name'  => $product->getName(), 
      'set'  => $product->getAttributeSetId(), 
      'type'  => $product->getTypeId(), 
      'category_ids'=> $product->getCategoryIds(), 
      'image_url_path' => $_image 
     ); 
    } 

    return $result; 
} 

希望它的工作 如果您有任何疑問告訴我,我會幫助你!

+0

謝謝Bhai ..它的工作就像一個魅力..我得到每一個URL。 –

1

你的代碼是真棒!

是的,你可以得到圖像的URL與:

'url_path' => Mage::getModel('catalog/product_media_config') 
       ->getMediaUrl($product->getImage());//getSmallImage(), getThumbnail() 

或另一個選項是調用:

$type = 'small_image'; 
'url_path' => $this->helper('catalog/image') 
        ->init($product, $type) 
        ->resize(163, 100); 

可以通過 '形象' small_image」或 '縮略圖'

改變

默認:

  • 基本映像:265x265像素

  • 小圖片:135x135像素

  • 縮略圖圖片:75x75像素


的更容易的選擇(詳細):

public function items($filters = null, $store = null) 
{ 
    $collection = Mage::getModel('catalog/product')->getCollection() 
     ->addStoreFilter($this->_getStoreId($store)) 
     ->addAttributeToSelect('name'); 

    if (is_array($filters)) { 
     try { 
      foreach ($filters as $field => $value) { 
       if (isset($this->_filtersMap[$field])) { 
        $field = $this->_filtersMap[$field]; 
       } 
       $collection->addFieldToFilter($field, $value); 
      } 
     } catch (Mage_Core_Exception $e) { 
      $this->_fault('filters_invalid', $e->getMessage()); 
     } 
    } 
    $result = array(); 

    foreach ($collection as $product) { 
     //$result[] = $product->getData(); 
      $full_product = Mage::getModel('catalog/product')->load($product_id); 

      $result[] = array(// Basic product data 
      'product_id' => $product->getId(), 
      'sku'  => $product->getSku(), 
      'name'  => $product->getName(), 
      'set'  => $product->getAttributeSetId(), 
      'type'  => $product->getTypeId(), 
      'category_ids'=> $product->getCategoryIds(), 
      'url_path' => $full_product->getImageUrl(), 
      // 'product_path' => $full_product->getProductUrl() 
      // you can call $full_product->getData(); 
     ); 
    } 
    return $result; 
} 
+1

其實我是一個Android開發人員,並且對PHP(Magneto)知之甚少......但在幾位同事的幫助下得到了答案,與您在此發佈的答案完全相同......將回答我自己的答案.Yours是更簡短的細節。感謝您的幫助。我會使用您分享的額外知識。再次感謝您非常多! –

+1

+1也是你的努力。 –

+0

謝謝@HareshChaudhary!我剛剛與Magento的Android的一些經驗。這就是爲什麼我知道這個代碼。大聲笑。很快我會問你關於android。 –

4

我最近和圖片一起工作,我相信我對它有很好的理解。

我不認爲Josua所說的是「真正的」正確答案。他的回答可以解決你的問題,但我不能忍受看到誤導性信息。

至於他的第一個選擇,它是「正確的」。

讓我打破代碼:

Mage_Catalog_Model_Product_Media_Config

public function getMediaUrl($file) 
{ 
    $file = $this->_prepareFileForUrl($file); 

    if(substr($file, 0, 1) == '/') { 
     return $this->getBaseMediaUrl() . $file; 
    } 

    return $this->getBaseMediaUrl() . '/' . $file; 
} 

public function getBaseMediaUrl() 
{ 
    return Mage::getBaseUrl('media') . 'catalog/product'; 
} 

protected function _prepareFileForUrl($file) 
{ 
    return str_replace(DS, '/', $file); 
} 

正如你所看到的,它只是簡單地添加media/ + catalog/product + $file

$文件是從數據庫中獲取,值會像/e/x/example.jpeg

你上傳的產品圖片存儲這些文件夾內。

現在,爲什麼$product-> getImageUrl()給你錯誤的URL的問題仍然未知。

的代碼Josua提出了第二個選項:

$this->helper('catalog/image') 
    ->init($product, $type) 
    ->resize(163, 100); 

是 「幾乎」 與$product->getImageUrl()一樣,它只是在resize

Mage_Catalog_Model_Product

public function getImageUrl() 
{ 
    return (string)$this->_getImageHelper()->init($this, 'image')->resize(265); 
} 

所以對於他的區別第二個選項,它會給你的舊代碼相同的結果。 我不知道他爲什麼要提出第二個選項,我覺得他從來沒有檢查什麼是那些功能(不是一個好主意,因爲它會導致錯誤的信息)的後面

當你致電$product->getImageUrl(),它會嘗試從緩存中加載圖像(如果不存在),它將從數據庫加載圖像(對於路徑,然後將從媒體文件夾中查找正確的圖像)並創建緩存。如果無法找到圖像或發生錯誤,則會獲取佔位符圖像。

我的建議是檢查是否有拋出的異常。您需要使用舊代碼$product->getImageUrl()。打開app/code/core/Mage/Catalog/Helper/Image.php

然後去:

Mage_Catalog_Helper_Image

public function __toString() 
{ 
    try { 
     if($this->getImageFile()) { 
      $this->_getModel()->setBaseFile($this->getImageFile()); 
     } else { 
      $this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir())); 
     } 

     if($this->_getModel()->isCached()) { 
      return $this->_getModel()->getUrl(); 
     } else { 
      if($this->_scheduleRotate) { 
       $this->_getModel()->rotate($this->getAngle()); 
      } 

      if ($this->_scheduleResize) { 
       $this->_getModel()->resize(); 
      } 

      if($this->getWatermark()) { 
       $this->_getModel()->setWatermark($this->getWatermark()); 
      } 

      $url = $this->_getModel()->saveFile()->getUrl(); 
     } 
    } catch(Exception $e) { 
     //put log to show error message 
     Mage::log($e->getMessage()); 
     $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder()); 
    } 
    return $url; 
} 

Mage::log($e->getMessage());登錄是否有拋出的異常。調用佔位符圖像很可能是因爲拋出了異常而被調用。

這只是從我一個建議,以確保沒有什麼錯你的圖像/其他事情,其實你已經直接解決您的問題,從media/catalog/product/...

另一個校正Josua的代碼獲取圖像:

注意$ full_product = Mage :: getModel('catalog/product') - > load($ product_id);

這是因爲裏面的foreach($集合作爲$產品),產品對象將被加載,所以產品的另一個負荷不必要完全沒有必要(也PRODUCT_ID $是不確定的)

UPDATE,剛剛殺青的代碼:

public function items($filters = null, $store = null) 
{ 
    $collection = Mage::getModel('catalog/product')->getCollection() 
     ->addStoreFilter($this->_getStoreId($store)) 
     ->addAttributeToSelect(array('name','image')); 
     //->addAttributeToSelect('name'); add another select, either image/small_image/thumbnail, modify it as you need 

    if (is_array($filters)) { 
     try { 
      foreach ($filters as $field => $value) { 
       if (isset($this->_filtersMap[$field])) { 
        $field = $this->_filtersMap[$field]; 
       } 
       $collection->addFieldToFilter($field, $value); 
      } 
     } catch (Mage_Core_Exception $e) { 
      $this->_fault('filters_invalid', $e->getMessage()); 
     } 
    } 
    $result = array(); 

    foreach ($collection as $product) { 
     //$result[] = $product->getData(); 
      $result[] = array(// Basic product data 
      'product_id' => $product->getId(), 
      'sku'  => $product->getSku(), 
      'name'  => $product->getName(), 
      'set'  => $product->getAttributeSetId(), 
      'type'  => $product->getTypeId(), 
      'category_ids'=> $product->getCategoryIds(), 
      'url_path' => $product-> getImageUrl() // Added the Method here 
     ); 
    } 
    return $result; 
} 
+2

你在哪裏正確,我已經檢查過,我把我引向錯誤。 –

+0

@HareshChaudhary,那麼錯誤是什麼?即使從達山的回答中,我也看不到解決方案。我只是想知道什麼是真正的問題和解決方案,因爲我在這裏學習不尋找點:) –

+0

Caras Keeper:我已經明確地定義了我在問題中的問題。關於錯誤,實現Josua的答案給了我Null - 客戶端上的指針異常意味着該數組未完全返回給客戶端。這意味着Code Block有一些錯誤,所以它不會返回它。在實現Darshan的答案時,它給了我圖像但緩存文件夾的URL路徑。在刪除MagentoStore的緩存時,會自動創建新的緩存,並且再次獲取我需要從圖像調用的圖像的URL客戶端。那就是我所要求的。 –

相關問題