在list.phtml中显示产品属性 - Magento
问题描述:
您好,我已阅读了许多关于此的帖子,虽然它的工作原理尚未完成。在list.phtml中显示产品属性 - Magento
例如;属性1 =鞋子和属性2 =鞋子颜色。 两者都处于下拉菜单中,我想列出类别页面中每个产品的所有可能的属性颜色。
问题:当我测试代码时,它将只显示第一只鞋的颜色,而不是所有的posibilites。我在这里做错了什么?
这里是我的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?>
<!-- ... -->
实施例2
<?php echo $_product->getResource()->getAttribute('shoecolor')->getFrontend()->getValue($_product) ?>
实施例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; ?>
答
下面是代码获得属性名和值,该值不属于任何产品
$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
答
另一种方式
$_product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
如果您创建一个像你属性 “shoesize”,那么你可以通过下面的代码访问。
如果你的属性类型文本字段($ _产品应加载):
<?php
echo $_product->getShoesize();
// if your arribute was shoe_size then
echo $_product->getShoeSize();
?>
如果你的属性类型多选择或下拉,让所有属性值:
<?php
echo $_product->getAttributeText('shoesize');
?>
答
你可以配置它的属性编辑页面
使用产品列表中 - >是
+1
最好的answare!由于该属性包含在$ _product中,因此我们不会启动另一个查询!大! – 2014-11-19 14:30:33
答
试试这个:
$_pp2 = Mage::getModel('catalog/product')->load( $_product->getId() );
echo $_pp2->getdm();
在:
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
属性代码:DM
Type: Text area
in view.phtml
echo $_product->get>getdm(); ?>
我不明白你的问题。你的意思是“列出所有可能的attibute颜色”?您应该看到您在产品页面中设置了可能的值。 – 2011-12-22 18:30:57