douguo6472 2013-01-10 14:08
浏览 117
已采纳

从自定义类别属性获取值

I am trying to get the value from a custom category attribute in Magento. The attribute is a select field and is been made with the install script below:

$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();

Unfortunately only getting numbers and not text value. I use this line to retrieve the value:

<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>

Can someone help me?

  • 写回答

3条回答 默认 最新

  • doufei1852 2013-01-10 14:15
    关注

    The sollution is pretty messy (the only one I know of).

    $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
    

    Didn't test it out, let me know if it works or not.

    PS: can't reply to your comment, not sure why. What does $opt contain ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写