在Magento中获取基础产品图片

16

我想在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。自己回答问题是完全可以的,但请将其作为实际答案发布,而不是放在问题本身内部。这样可以使答案被投票/接受,并帮助我们保持“未回答”列表更清晰。 - Jürgen Thelen
7个回答

21

我认为您正在寻找这个:

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

应该给予BenMarks信用,他提供了这个答案


2
$product->getImage() 返回 null。小图和缩略图正常工作。这是否与其在 Mage_Checkout_Block_Cart_Sidebar 中被调用有关? - Dave
如果您的购物车中有 $item 作为产品,请尝试以下操作:$product = Mage::getModel('category/product')->load($item->getId()); echo Mage::getModel('catalog/product_media_config') ->getMediaUrl( $product->getImage() ); - Jerzy Zawadzki
$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId()); 最终它按预期工作了。谢谢。 - Dave
这很适合只有图像 URL 的情况。但据我所知,您无法将其调整为任何您想要的大小 :( - Joshua Pack

3

注意!

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

它是一个对象,而不是一个URL字符串。是的,你可以直接使用echo来输出它,但不应该将它赋值给变量。例如,下面这个不能正常工作

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

使用foreach后,你只会保存最后一个图片的url。获取真正的字符串url的简单方法是:

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

非常感谢!你帮了我大忙。我将它转换为字符串,而不是添加 . ''。不确定这是否是最佳实践。 - Alex Lacayo
2
我猜更明显的转换为字符串可能会更清晰 - $images[] = $images_obj.'' >>> $images[] = (string)$images_obj; - Zippp

3

尝试:

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

2
这种情况发生的原因是产品列表中未加载image属性。通常情况下,您可以在编辑属性时更改此设置,但对于此属性,您无法编辑这些设置。我认为这是因为它是一个库存属性。
简而言之:
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;

警告,只有当你确定catalog_product实体的image属性ID为106时,才应执行此查询!

一些答案建议使用以下方法:

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

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

你不应该这样做!因为这将会进行完整的产品加载!这不是高效的,而且很可能在循环内部执行,这更糟糕!抱歉我刚才大声了!

通常我不赞成直接编辑数据库,但在这种情况下,这是最简单的解决方案:

# 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设置脚本。


1
小图像和缩略图效果很好。然后尝试使用small_image代替image,就像这样:
echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);

3
我想获取基础图像,而不是小图像。这对我很重要。 - Dave
@marco请看下面我的回答 - nick.graziano

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).'';

对我来说有效....


将其附加到字符串中有些模糊,最好是进行类型转换。 $product_image = (string) Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height); - Ben
1
@Ben 我在末尾添加了 .'',现在请检查我的答案。 - Manikandan Arunachalam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接