Php 从自定义类别属性获取值
我试图从Magento中的自定义类别属性中获取值。该属性是一个选择字段,由以下安装脚本创建:Php 从自定义类别属性获取值,php,magento,Php,Magento,我试图从Magento中的自定义类别属性中获取值。该属性是一个选择字段,由以下安装脚本创建: $this->startSetup(); $this->addAttribute('catalog_category', 'category_categorycolor', array( 'group' => 'General Information', 'input' => 'select', 'type'
$this->startSetup();
$this->addAttribute('catalog_category', 'category_categorycolor', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Categorie kleur',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'option' => array (
'value' => array('yellow' => array('Geel'),
'purple' => array('Paars'),
'blue' => array('Blauw'),
'red' => array('Rood'),
'orange' => array('Oranje'),
'green' => array('Groen'),
'darkblue' => array('Donkerblauw'),
'lightgreen' => array('Lichtgroen'),
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
不幸的是,只得到数字而不是文本值。我使用此行检索值:
<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>
有人能帮帮我吗?解决方案很混乱,这是我唯一知道的
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
没有测试出来,让我知道它是否有效
PS:无法回复您的评论,不确定原因。$opt包含什么?解决方案非常混乱,这是我唯一知道的解决方案
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
没有测试出来,让我知道它是否有效
PS:无法回复您的评论,不确定原因。$opt包含什么?类似于以下内容:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
大概是这样的:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
返回的数字是下拉列表中每个值的id。您还必须加载下拉列表值 见下页。它帮助我理解了这一点
返回的数字是下拉列表中每个值的id。您还必须加载下拉列表值 见下页。它帮助我理解了这一点
几乎我需要当前类别ID,而不是特定的类别ID。Mage::registry'current_category'存储您当前的类别。我需要当前的类别ID而不是特定的。Mage::registry'current_category'存储您当前的类别hi Vlad。它包含以下内容:array8{[11]=>string5 Blauw[15]=>string11 Donkerblauw[9]=>string4 Geel[14]=>string5 Groen[16]=>string10 Lichtgroen[13]=>string6 Oranje[10]=>string5 Paars[12]=>string4 Rood}Hi Vlad。是我的错。错误的回声:-它与Hi-Vlad一起工作。它包含以下内容:array8{[11]=>string5 Blauw[15]=>string11 Donkerblauw[9]=>string4 Geel[14]=>string5 Groen[16]=>string10 Lichtgroen[13]=>string6 Oranje[10]=>string5 Paars[12]=>string4 Rood}Hi Vlad。是我的错。错误的回声:-它正在与