检索属性代码

时间:2010-07-27 08:05:25

标签: magento

一件让我疯狂的小事:)

我无法在/catalog/product/view/type/options/configurable.phtml模板文件中检索当前属性的属性代码。

当我改变

时,在第36行周围(或在第36行)
echo $_attribute->getLabel()

echo $_attribute->getAttributeId()

我得到了数据库的eav_attribute表中存在的正确属性Id。 但是当我尝试

echo $_attribute->getAttributeCode()

我得到一个空字符串,而attribute_code表中有一个eav_attribute字段。

你能帮我找一下属性的属性代码吗?或者,更一般地说:如何获取我们现在为id的属性的属性代码?

非常感谢!

6 个答案:

答案 0 :(得分:15)

在这种情况下,无需重载Magento基本代码。 如果你print_r($ _ attribute)你可以看到属性代​​码在array()中更深,你可以这样直接在你的模板文件中使用它:

echo $_attribute->getProductAttribute()->getAttributeCode();

答案 1 :(得分:4)

试试

$_attribute_code = Mage::getModel('eav/entity_attribute')->load($_attribute->getAttributeId())->getAttributeCode();

答案 2 :(得分:1)

/**
 * get attribute collection
 */
$attribute = $_product->getResource()->getAttribute('my_attribute');
/**
 * get attribute type
 */
$attribute->getAttributeType();
/**
 * get attribute Label
 */
$attribute->getFrontendLabel();
/**
 * get attribute default value
 */
$attribute->getDefaultValue();
/**
 * check if the attribute is visible
 */
$attribute->getIsVisible();
/**
 * check if the attribute is required
 */
$attribute->getIsRequired();
/**
 * get attribute value
 */
$attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getMyAttribute();

以下是有关获取属性名称,代码和值的详细代码和说明:http://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-value/

答案 3 :(得分:0)

我刚刚在magento安装上尝试了该代码,并返回正确的属性代码。您可以随时尝试直接获取数据:

echo $_attribute->attribute_code;

但我不认为这会有所帮助。在var_dump变量上尝试print_r$_attribute,看看数据是否真的丢失。

答案 4 :(得分:0)

所以找到了一种方式,我确信这不是最好的方法...或者Magento团队可能认为不需要根据属性ID获取属性代码......

1- 重载 app / code / core / Mage / Eav / Model / Mysql4 / Entity / Attribute / Collection.php

添加此方法:

public function setIdFilter($id)
    {
        if (empty($id)) {
            return $this;
        }
        if (!is_array($id)) {
            $id = array($id);
        }
        $this->getSelect()->where('main_table.attribute_id IN(?)', $id);
        return $this;
    }

2-现在根据id的使用来检索属性的代码:

$attributeCode = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setIdFilter(YOUR_ATTRIBUTE_ID) /* if using this into the configurable.phtml template file, you can use $_attribute->getAttributeId() for YOUR_ATTRIBUTE_ID */
        ->getFirstItem()
        ->getAttributeCode();
echo $attributeCode;

如此单一但却如此有效......

如果有人有更好的方法,我很乐意改变我的“回答代码”!

答案 5 :(得分:0)

老问题,但是自从谷歌出现以来我想提供一个答案。

我能用这个php获取属性代码:

$productAttributeOptions = $_product->getTypeInstance(true)
  ->getConfigurableAttributesAsArray($_product);
print_r($productAttributeOptions);
PS,Magento非常令人沮丧。

相关问题