简体   繁体   English

Magento - 在view.phtml中获取产品项属性

[英]Magento - Get product items attribute in view.phtml

I am trying to load a custom category in the catalog/category/view.phtml to archive this I use: 我正在尝试在catalog / category / view.phtml中加载自定义类别来存档我使用的:

<?php
$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
    foreach( $_productCollection as $_product ):
        echo $_product->getProductUrl();
        echo $this->getPriceHtml($_product, true);
        echo $this->htmlEscape($_product->getName());
    endforeach;
}
?>

I can load the URL for example, now I want to load a custom attribute for example color: 我可以加载URL,例如,现在我想加载一个自定义属性,例如颜色:

$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)

This code does not work, I am 100% sure the color attribute is set to show in the category listing and also that the items in this category have this fields fill. 此代码不起作用,我100%确定颜色属性设置为在类别列表中显示,并且此类别中的项目已填写此字段。 I know this because this code works on list.html . 我知道这个,因为这段代码适用于list.html

What I am doing wrong? 我做错了什么? I am working with 1.7.0.2. 我正在使用1.7.0.2。

The expected result is to show all COLOR attibutes from a custom cateogory in 预期的结果是显示来自自定义类别的所有COLOR属性

catalog/category/view.phtml 目录/类别/ view.phtml

I can't believe I just found the answer. 我简直不敢相信我找到了答案。 Because we are not in a regular category listing we need to add the custom attributes to the collection. 因为我们不在常规类别列表中,所以我们需要将自定义属性添加到集合中。

Here is the code: 这是代码:

$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');

If "color" is in the flat table you should be able to 如果平面表中的“颜色”你应该能够

$_product->getColor();

If this attribute is not in the collection, you can either add it to the flat table by making the attribute filterable, add it in the PHP collection call 如果此属性不在集合中,您可以通过使属性可过滤将其添加到平面表中,将其添加到PHP集合调用中

$_productCollection = $_category->getProductCollection()
    ->addAttributeToSelect('color');

Or load the product model to get all of the attributes 或者加载产品模型以获取所有属性

$_product = Mage::getModel('catalog/product')->load($_product->getId());
echo $_product->getColor();
  1. Make this attribute visible in listing/frontend 在listing / frontend中显示此属性

  2. Run Reindexing 运行重建索引

  3. foreach( $_productCollection as $_product ): echo $_product->getProductUrl();

in this code var_dump $_product->getData(); 在此代码中var_dump $_product->getData(); check if this var_dump results in that specific attribute value getting displayed. 检查此var_dump导致显示该特定属性值。

Note: 注意:

$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)

is not a very efficient way of calling. 不是一种非常有效的通话方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM