php 获取 magento 中所有产品属性的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4961117/
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
get an array of all a products attributes in magento
提问by Chad
I cannot figure this out!
我想不通!
I am trying to get a list of a products attributes into an array on the list.phtml page. I have tried everything. I have seen a lot of solutions that use
我正在尝试将产品属性列表放入 list.phtml 页面上的数组中。我已经尝试了一切。我见过很多使用的解决方案
$attributes = $product->getAttributes();
but I cannot get this to work, it just brings up a blank page. Any help would be greatly appreciated, I have spent hours and hours on this so far...
但我无法让它工作,它只会显示一个空白页面。任何帮助将不胜感激,到目前为止,我已经在这方面花费了数小时和数小时......
I am using Magento version 1.4.2.0
我使用的是 Magento 版本 1.4.2.0
UPDATE: Here is exactly what I am trying to do:
更新:这正是我想要做的:
$neededAttributes = Mage::helper('mymodule')->getNeededAttributes();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if(in_array($attribute->getAttributeCode(), $neededAttributes)) {
$attributename = $attribute->getAttributeCode();
echo $attributename;
}
}
this is in the file gallery.phtml in design/adminhtml/default/default/catalog/product/helper/
这是在 design/adminhtml/default/default/catalog/product/helper/ 中的文件 gallery.phtml 中
For some reason, I cannot get the getAttributeCode function to return anything.
出于某种原因,我无法让 getAttributeCode 函数返回任何内容。
回答by clockworkgeek
I'm guessing you need a list of only visible values. I say "values" because attributes are not the actual values, they are descriptors. The following is the salient parts from Mage_Mage_Catalog_Block_Product_View_Attributes
:
我猜你只需要一个可见值的列表。我说“值”是因为属性不是实际值,它们是描述符。以下是来自的突出部分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
}
}
You don't really need to duplicate this though since you can alter/use the template catalog/product/view/attributes.phtml
which is already declared on the product view page as attributes
block.
您实际上不需要复制它,因为您可以更改/使用catalog/product/view/attributes.phtml
已经在产品视图页面上声明为attributes
块的模板。
回答by nevvermind
According to your question, you should be using Mage::getResourceModel('catalog/product_attribute_collection')
instead:
根据您的问题,您应该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());
}
You don't always have attributes in the _data
(getData()
) storage and you don't always need to load a product to get its attributes.
您并不总是在_data
( getData()
) 存储中拥有属性,并且您并不总是需要加载产品来获取其属性。
回答by Anton S
It's rather easy and gives you an array of available product attribute names
它相当简单,并为您提供了一系列可用的产品属性名称
$product = Mage::getModel('catalog/product')->load('product_id');
$attributeNames = array_keys($product->getData());
print_r($attributeNames);
If you need a attribute object collection you can call
如果你需要一个属性对象集合,你可以调用
$product->getAttributes();
If you need a product collection and after that you can perform the previously mentioned ways on each collection member
如果您需要一个产品集合,然后您可以对每个集合成员执行前面提到的方法
Mage::getModel('catalog/product')->getCollection();