我正在使用Magento的新“按产品过滤”模块,我有一种情况,我应该检索所有属性及其值。我用Google搜索并找到以下代码
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->load();
// ->addFieldToFilter('attribute_code', 'color')
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
/* @var $attribute Mage_Eav_Model_Entity_Attribute */
$attr = $attribute->getSource()->getAllOptions(true);
foreach ($attr as $att) {
echo " Label : ".$att['label']." Value : ".$att['value']."";
}
但是这只从所有可用属性的列表中检索最后一个属性的标签和值。
如何获得所有属性?我在这段代码中做错了什么?
谢谢, 巴兰
答案 0 :(得分:14)
试试这个:
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
// Localize attribute label (if you need it)
$attributes->addStoreLabel(Mage::app()->getStore()->getId());
// Loop over all attributes
foreach ($attributes as $attr) {
/* @var $attr Mage_Eav_Model_Entity_Attribute */
// get the store label value
$label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
echo "Attribute: {$label}\n";
// If it is an attribute with predefined values
if ($attr->usesSource()) {
// Get all option values ans labels
$options = $attr->getSource()->getAllOptions();
// Output all option labels and values
foreach ($options as $option)
{
echo " {$option['label']} (Value {$option['value']})\n";
}
}
else
{
// Just for clarification of the debug code
echo " No select or multiselect attribute\n";
}
}
答案 1 :(得分:0)
这是第一个解决方案:
$products = Mage::getModel('catalog/product')->getCollection();
foreach($this->products as $product) {
$prod = Mage::getModel('catalog/product')->load($product->getId());
}
如果您正在使用来自magento外部的数据,您可以使用我制作的课程: http://blog.alexparadise.com/magento-dbeav-class/
它是测试版,但这应该适用于您的情况。 或者获取EAV表的概念......并进行自己的SQL查询。