获取magento中所有产品属性的数组

发布于 2024-10-17 00:34:06 字数 736 浏览 2 评论 0原文

我无法弄清楚这一点!

我正在尝试将产品属性列表放入 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/default/default/catalog/product/helper/

由于某种原因,我无法让 getAttributeCode 函数返回任何内容。

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

$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

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/

For some reason, I cannot get the getAttributeCode function to return anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-10-24 00:34:06

我猜你需要一个仅可见值的列表。我说“值”是因为属性不是实际值,它们是描述符。以下是 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
    }
}

不过,您实际上不需要重复此内容,因为您可以更改/使用模板 catalog/product/view/attributes.phtml它已在产品视图页面上声明为 attributes 块。

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:

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

命硬 2024-10-24 00:34:06

根据您的问题,您应该改用 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());
}

您并不总是在 _data 中具有属性 (getData()) 存储,您并不总是需要加载产品来获取其属性。

According to your question, you should be using Mage::getResourceModel('catalog/product_attribute_collection') instead:

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

绻影浮沉 2024-10-24 00:34:06

它相当简单,并为您提供了一组可用的产品属性名称

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

如果您需要一个属性对象集合,您可以调用

$product->getAttributes();

如果您需要一个产品集合,然后您可以对每个集合成员执行前面提到的方法

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文