我想在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);
我想你正在寻找这个:
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()
应该感谢给出这个答案的BenMarks。
尝试:
$this->helper('catalog/image')->init($_product, 'image')->keepFrame(false)
->constrainOnly(true)->resize(38,38);
警惕!
$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;
}
发生这种情况的原因是产品列表中未加载image
属性。通常,您可以在编辑属性时更改此设置,但无法编辑此属性的这些设置。我认为这是因为它是一个股票属性。
TLDR;
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;
** 警告,在确定catalog_product
实体的image
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安装脚本。
小图像和缩略图效果很好。
然后尝试small_image而不是图像,如下所示:
echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);
<img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=''225'' height=''225''/>
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).'';
它对我有用。