php 在 list.phtml 中显示产品属性 - Magento

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8604222/
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-26 05:03:44  来源:igfitidea点击:

Show product attributes in list.phtml - Magento

phparraysmagentofor-loopconfigurable-product

提问by Ruud

Hello I have read many posts about this, and while it works its not complete.

你好,我已经阅读了很多关于这个的帖子,虽然它有效但并不完整。

For example; Attribute 1= shoesize and attribute 2 = shoe color. Both are in a dropdown and I would like to list all of the possible attribute colors per product within the category pages.

例如; 属性 1 = 鞋码,属性 2 = 鞋色。两者都在下拉列表中,我想在类别页面中列出每个产品的所有可能的属性颜色。

Problem: When I test the code it will only display the first shoe color, instead of all posibilites. What am I doing wrong here?

问题:当我测试代码时,它只会显示第一个鞋子颜色,而不是所有可能性。我在这里做错了什么?

Here are 3 examples of what I have. All code work, but only shows the first attribute color. Example 1:

以下是我所拥有的 3 个示例。所有代码都有效,但只显示第一个属性颜色。示例 1:

<!-- Find the following loop -->
<?php foreach ($_productCollection as $_product): ?>
<!-- Inside it insert one of the following codes as needed -->
<!-- Use this for regular text attributes -->
<?php echo $_product->getMyAttribute() ?>
<?php echo $_product->getAnotherCustomAttribute() ?>

<!-- Use this for dropdown attributes -->
<?php echo $_product->getAttributeText('shoecolor') ?>
<?php endforeach?>
<!-- ... --> 

Example 2

示例 2

<?php echo $_product->getResource()->getAttribute('shoecolor')->getFrontend()->getValue($_product) ?>

Example 3

示例 3

<?php $type = "simple"; $p = "0" ?> 
<?php foreach ($_productCollection as $_product): ?> 
<?php $custom = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); ?> 
<?php $col = $custom->getUsedProductCollection()->addAttributeToSelect('shoecolor')->addFilterByRequiredOptions(); ?> 
<?php foreach($col as $simple_product) { $p=$simple_product->getId(); $type="configurable"; } ?> 

<?php if($type == "configurable"): ?> 
<h5><?php echo $_product->load($p)->getAttributeText('shoecolor'); ?><?php $type="simple" ?></h5> 
 <?php endif; ?> 

回答by cindy

you can just config it in attribute edit page

您可以在属性编辑页面中配置它

Used in Product Listing -> Yes

用于产品列表 -> 是

回答by O?uz ?elikdemir

Another way

其它的办法

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

If you create an attribute like yours "shoesize" then you can access by following code.

如果你创建了一个像你的“鞋码”这样的属性,那么你可以通过以下代码访问。

If your attribute type Text Field ( $_product should loaded ) :

如果您的属性类型文本字段(应加载 $_product ):

<?php
  echo $_product->getShoesize();
  // if your arribute was shoe_size then
  echo $_product->getShoeSize();
?>

If your attribute type Multiple Select or Dropdown, to get all attribute value :

如果您的属性类型为 Multiple Select 或 Dropdown,要获取所有属性值:

<?php
   echo $_product->getAttributeText('shoesize');
?>

回答by O?uz ?elikdemir

Here is the code get attribute name and value that that doesn't belongs to any product

这是获取不属于任何产品的属性名称和值的代码

$attributeCode = 'YOUR_ATTRIBUTE_CODE';

$product = Mage::getModel('catalog/product');

$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
   ->setEntityTypeFilter($product->getResource()->getTypeId())
   ->addFieldToFilter('attribute_code', $attributeCode);

$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes

$options = $attribute->getSource()->getAllOptions(false);
print_r($options); // print out attribute options

回答by user956584

Try this:

尝试这个:

$_pp2 = Mage::getModel('catalog/product')->load(    $_product->getId()    );
echo $_pp2->getdm();

in:

在:

 <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>

Attribute Code:dm

属性代码:dm

Type: Text area

in view.phtml

在view.phtml

echo $_product->get>getdm(); ?>