2011-12-22 193 views
4

您好,我已閱讀了許多關於此的帖子,雖然它的工作原理尚未完成。在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; ?> 
+0

我不明白你的問題。你的意思是「列出所有可能的attibute顏色」?您應該看到您在產品頁面中設置了可能的值。 – 2011-12-22 18:30:57

回答

1

下面是代碼獲得屬性名和值,該值不屬於任何產品

$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 
6

另一種方式

$_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'); 
?> 
14

你可以配置它的屬性編輯頁面

使用產品列表中 - >是

+1

最好的answare!由於該屬性包含在$ _product中,因此我們不會啓動另一個查詢!大! – 2014-11-19 14:30:33

0

試試這個:

$_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(); ?> 
相關問題