2012-08-16 24 views
15

我想要獲得基地 Magento中的產品圖片以調整其大小並顯示在購物車邊欄中。在Magento中獲取基礎產品圖片

可惜的是這樣的:

echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38); 

打印Magento的圖像佔位符。

正確設置本產品的基礎圖像。小圖像和縮略圖效果很好。

不知道發生了什麼事。

編輯: 解決方案: 獲取完整的產品數據是這樣的:

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

,然後用它作爲你希望:

echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38); 
+7

歡迎來到Stack Overflow。這是絕對[自行](http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions/12519#12519)你自己的問題,但請張貼它作爲實際答案而不是問題本身。這使答案被投票/接受,並幫助我們保持「未答覆」清單更清晰。 – 2012-08-17 01:16:53

回答

3

嘗試:

$this->helper('catalog/image')->init($_product, 'image')->keepFrame(false) 
->constrainOnly(true)->resize(38,38); 
22

我認爲你是look。納克這樣的:

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

信貸應給予BenMarks誰給了這個answer

+1

'$ product-> getImage()'返回'null'。用小和縮略圖的作品。它是否在** Mage_Checkout_Block_Cart_Sidebar **中被調用? – Dave 2012-08-16 21:01:45

+0

如果您將$ item作爲購物車中的產品,請先嚐試: '$ product = Mage :: getModel('category/product') - > load($ item-> getId()); 回波法師:: getModel( '目錄/ product_media_config') - > getMediaUrl($產品 - >的getImage());' – 2012-08-16 21:04:18

+0

耶日,幾乎:) '$ _產品=法師:: getModel('目錄/產品的) - > load($ _ item-> getProduct() - > getId());' 最後它按預期工作。 Dzięki。 – Dave 2012-08-16 21:24:51

1

小圖像和縮略圖效果很好。

然後嘗試small_image而不是圖像,像這樣:

echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38); 
+2

但我想獲得基本形象,而不是small_image。這對我很重要 – Dave 2012-08-16 21:04:25

+2

有人知道爲什麼'圖像'將無法工作? – 2015-01-23 08:00:09

+0

@ marco看到我的回答低於 – 2017-04-14 22:59:46

3

注意!

$this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38); 

對象不是URL字符串它的自我。是的,你可以直接使用echo,但不應該將其分配給var。例如,這不會工作

$images = array(); 
    foreach($products as $_product){ 
     $images[]=$this->helper('catalog/image')->init($_product, 'small_image') 
     ->resize(38, 38); 
    } 

的foreach後,你將有隻有一個最後圖像的URL保存。簡單的方法是讓真正的字符串網址是:

$images = array(); 
foreach($products as $_product){ 
    $images_obj = $this->helper('catalog/image')->init($_product, 'small_image') 
    ->resize(38, 38); 
    $images[] = (string)$images_obj; 
} 
+0

非常感謝!幫助我出去了一噸,而不是添加。「我將它轉換爲(字符串)。不知道是否是最佳做法。 – 2014-09-21 00:08:37

+2

我想更明顯的轉換爲字符串可能會更清晰一點 - $ images [] = $ images_obj。''>>> $ images [] =(string)$ images_obj; – Zippp 2014-10-31 08:59:29

0
<img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=\'225\' height=\'225\'/> 
0

Magento的產品圖片分配給變量

$product_image = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';

Magento的產品圖片分配給對象

$products[] = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';

它適用於我....

+0

將它附加到一個字符串有點不明確,反而會更好地對它進行類型轉換。'$ product_image =(string)Mage :: helper('catalog/image') - > init($ productmodel,'small_image') - > keepFrame(true) - > resize(width,height);' – Ben 2014-11-03 04:26:44

+1

@Ben我有在最後加上'''',現在檢查我的答案。 – 2014-12-18 07:28:11

0

發生這種情況的原因是因爲image屬性未加載到產品列表中。通常,您可以在編輯屬性時更改此設置,但不能編輯這些屬性的設置。我認爲這是因爲它是一種股票屬性。

TLDR;

UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106; 

**警告,你不應該直到你一定執行這個^^^查詢您的image attribute_id爲catalog_product實體是106!

一些答案表明這種方法:

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

echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38); 

你不應該這樣做!這是因爲你會做一個完整的產品負載!這是不高效的,而且很可能是在更糟的循環內完成的!對不起尖叫!

我通常不縱容直接DB編輯,但在這種情況下,對我來說是最簡單的解決方案:

# First make sure we are using all the right IDs, who knows, I have seen some fubar'ed deployments 

SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product'; 
# 10 
SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'image' AND entity_type_id = 10; 
# 106 

# Now that we know the exact attribute_id.... 
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106; 

現在image屬性數據將被自動地加載在產品列表頁面,那麼你就可以訪問它是這樣的:

echo $this->helper('catalog/image')->init($_product, 'image'); 

最好的部分是,你沒有加載整個產品的循環!千萬不要這麼做

**另外,因爲我知道我會讓人們說這不是Magento的方式,所以替代方法是創建一個具有運行命令的SQL安裝腳本的模塊。