2011-02-10 119 views
13

我無法弄清楚這一點!得到一個magento中的所有產品屬性的數組

我想獲取產品屬性列表到list.phtml頁面的數組中。我已經嘗試了一切。我看到很多使用的解決方案

$attributes = $product->getAttributes(); 

但我不能得到這個工作,它只是提出了一個空白頁。任何幫助將不勝感激,我已經花了幾個小時在這個迄今爲止...

我使用的Magento版本1.4.2.0

更新:這正是我想要做的事:

$neededAttributes = Mage::helper('mymodule')->getNeededAttributes(); 
$attributes = $product->getAttributes(); 
foreach ($attributes as $attribute) { 
    if(in_array($attribute->getAttributeCode(), $neededAttributes)) { 
     $attributename = $attribute->getAttributeCode(); 
    echo $attributename; 
    } 
} 

這是在設計文件gallery.phtml/adminhtml /默認/缺省/目錄/產品/幫手/

出於某種原因,我不能讓getAttributeCode函數返回任何東西。

+0

您是否試過先獲取產品收藏? – kjy112 2011-02-10 19:21:15

+1

你有沒有像XDebug一樣使用調試器進行逐步調試? – greg0ire 2011-02-10 19:54:33

+1

您是在詢問特定產品的屬性值,還是所有可能的屬性代碼列表? – 2011-02-10 22:36:03

回答

23

我猜你需要一個只有可見值的列表。我說「價值觀」,因爲屬性不是實際值,它們是描述符。以下是從Mage_Mage_Catalog_Block_Product_View_Attributes突出部分:

$attributes = $product->getAttributes(); 
foreach ($attributes as $attribute) { 
    if ($attribute->getIsVisibleOnFront()) { 
     $value = $attribute->getFrontend()->getValue($product); 
     // do something with $value here 
    } 
} 

你並不真的需要複製這雖然因爲你可以改變/使用已經宣告產品視圖頁面上attributes塊模板catalog/product/view/attributes.phtml

6

這是相當容易的,給你提供的產品陣列屬性名

$product = Mage::getModel('catalog/product')->load('product_id'); 
$attributeNames = array_keys($product->getData()); 
print_r($attributeNames); 

如果你需要一個屬性對象集合,你可以調用

$product->getAttributes(); 

如果您需要的產品集合,之後你可以對每個收集成員執行前面提到的方法

Mage::getModel('catalog/product')->getCollection(); 
+0

可能還要注意這一點,你需要設置「產品清單中使用」>是屬性設置 – 2015-01-14 11:55:07

14

根據你的問題,你應該使用Mage::getResourceModel('catalog/product_attribute_collection')代替:

$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection'); 

foreach ($productAttrs as $productAttr) { /** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */ 
    var_dump($productAttr->getAttributeCode()); 
} 

你並不總是必須在_datagetData())存儲屬性,你並不總是需要加載一個產品獲得其屬性。

相關問題