php 在 Magento 中获取基本产品图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11995206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:36:40  来源:igfitidea点击:

Get base product image in Magento

phpimagemagentoshopping-cart

提问by Dave

I want to get baseproduct image in Magento to resize it and display in cart sidebar.

我想在 Magento 中获取基本产品图像以调整其大小并显示在购物车侧栏中。

Unfortunatelly this:

不幸的是:

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

prints Magento placeholder image.

打印 Magento 占位符图像。

Base image is set for this product properly. Small image and thumbnail works great.

为该产品正确设置了基本图像。小图像和缩略图效果很好。

No idea what's going on.

不知道发生了什么。

EDIT:Solution: Get full product data this way:

编辑:解决方案:通过这种方式获取完整的产品数据:

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

and then use it as you wish:

然后根据需要使用它:

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

回答by pzirkind

I think you are looking for this:

我想你正在寻找这个:

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

Credit should be given to BenMarkswho gave this answer.

应该归功于给出这个答案的BenMarks

回答by alfasin

Try:

尝试:

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

回答by Paulius

BE AWARE!

意识到!

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

is object, not url stringit self. Yes, you can use it directly with echo, but shouldn't assign it to var. For example this wont work:

object而不是它自己的url 字符串。是的,您可以直接将其与 echo 一起使用,但不应将其分配给 var。例如这行不通

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

After foreach, you will have only one last image url saved. Simple way is to get truly string url is:

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

回答by nick.graziano

The reason this is happening is because the imageattribute is not loaded in the product listing. Normally you can change this while editing the attribute, but you can't edit those settings for this attribute. I think that's because it is a stock attribute.

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

TLDR;

TLDR;

UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;

** Warning, you should not execute this ^^^ query until you are certain your imageattribute_id for the catalog_productentity is 106!

** 警告,在确定实体的imageattribute_id 为catalog_product106之前,不应执行此 ^^^ 查询!

Some answers are suggesting this method:

一些答案建议使用这种方法:

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

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

You should not do this! This is because you will do a full product load! This is not efficient, and is most likely being done inside of a loop which is even worse! Sorry for screaming!

你不应该这样做!这是因为您将执行完整的产品加载!这效率不高,而且很可能是在更糟糕的循环内完成的!对不起尖叫!

I usually do not condone direct DB edits, but in this case it was the easiest solution for me:

我通常不会容忍直接的数据库编辑,但在这种情况下,它对我来说是最简单的解决方案:

# 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;

Now the imageattribute data will be automagically loaded on the product listing pages, then you can access it like this:

现在image属性数据将自动加载到产品列表页面上,然后您可以像这样访问它:

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

The best part is that you are not loading the entire product in a loop! DO NOT EVER DO THAT EVER

最好的部分是您不会循环加载整个产品!永远不要那样做

** Also, because I know I am going to get people saying that this is not the Magento way, the alternative would be to create a module that has an SQL setup script that runs the command.

** 另外,因为我知道我会让人们说这不是 Magento 的方式,替代方法是创建一个模块,该模块具有运行命令的 SQL 安装脚本。

回答by ADev

Small image and thumbnail works great.

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

Then try small_image instead of image, like this:

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

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

回答by Arthur Yakovlev

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

回答by Manikandan Arunachalam

Magento Product image assigning to Variable

分配给变量的 Magento 产品图像

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

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

Magento Product image assigning to Object

分配给对象的 Magento 产品图像

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

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

It Works for me....

这个对我有用....