获取Magento的基本产品图片

时间:2012-08-16 20:48:44

标签: php image magento shopping-cart

我希望在Magento中获得 base 产品图片以调整其大小并显示在购物车边栏中。

不幸的是:

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 个答案:

答案 0 :(得分:21)

我认为你正在寻找这个:

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

应该给予BenMarks answer的{{3}}信用。

答案 1 :(得分:3)

尝试:

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

答案 2 :(得分:3)

BE AWARE!

$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;
}

答案 3 :(得分:1)

  

小图片和缩略图效果很好。

然后尝试使用small_image而不是image,如下所示:

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

答案 4 :(得分:1)

发生这种情况的原因是因为产品列表中未加载image属性。通常,您可以在编辑属性时更改此设置,但无法为此属性编辑这些设置。我认为这是因为它是股票属性。

TLDR;

UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;

**警告,在确定image实体的catalog_product attribute_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安装脚本的模块。

答案 5 :(得分:0)

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

答案 6 :(得分: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).'';

它对我有用....