2012-05-11 48 views
0

我在我的頭版上有一個塊,它用一個名爲image_feature_front_right的自定義產品圖像來抓取兩個最新產品。在Magento中抓取自定義產品圖片標籤?

我用這個代碼查詢:

$_helper = $this->helper('catalog/output'); 
$_productCollection = Mage::getModel("catalog/product")->getCollection(); 
$_productCollection->addAttributeToSelect('*'); 
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1)); 
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection')); 
$_productCollection->addAttributeToSort('updated_at', 'DESC'); 
$_productCollection->setPageSize(2); 

我能夠得到:

  • 圖像:echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4)
  • 產品網址:echo $_product->getProductUrl()
  • 產品名稱: $this->stripTags($_product->getName(), null, true)

我無法得到的唯一的東西是自定義圖像標籤。我試過echo $this->getImageLabel($_product, 'image_feature_front_right'),但這似乎什麼都不做。

任何人都可以指向正確的方向嗎?

謝謝!

Tre

回答

3

我想這是某種magento錯誤。這個問題似乎是Magento核心沒有設置custom_image_label屬性。而對於默認的內置圖像[圖像,small_image,thumbnail_image]它不設置這些屬性 - 所以你可以這樣做:

$_product->getData('small_image_label'); 

如果你看看Mage_Catalog_Block_Product_Abstract::getImageLabel()它只是追加「_label」的$mediaAttributeCode那你作爲第二參數傳入並呼叫$_product->getData()

如果您致電$_product->getData('media_gallery');,您會看到自定義圖像標籤可用。它只是嵌套在一個數組中。因此,使用此功能:

function getImageLabel($_product, $key) { 
    $gallery = $_product->getData('media_gallery'); 
    $file = $_product->getData($key); 
    if ($file && $gallery && array_key_exists('images', $gallery)) {  
     foreach ($gallery['images'] as $image) { 
      if ($image['file'] == $file) 
       return $image['label']; 
     } 
    } 
    return ''; 
} 

這將會是謹慎地擴展Magento的核心代碼(理想情況下Mage_Catalog_Block_Product_Abstract,但我不認爲Magento的您可以重寫抽象類),但是如果你需要一個快速的黑客 - 只是將此功能粘貼到您的phtml文件中,然後調用:

<?php echo getImageLabel($_product, 'image_feature_front_right')?>