如何动态地为magento中的多选属性添加值?

时间:2013-10-18 09:47:27

标签: php magento attributes

我使用以下代码在我的自定义模块Form

中添加multiselect属性
$fieldset->addField('multiselect2', 'multiselect', array(
      'label'     => Mage::helper('checkout')->__('Select Type2'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
      'onclick' => "return false;",
      'onchange' => "return false;",
      'value'  => '4',
      'values' => array(
                            '-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),
                            '1' => array(
                                            'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
                                            'label' => 'Size'    
                                       ),
                            '2' => array(
                                            'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                            'label' => 'Color'   
                                       ),                                         

                       ),
      'disabled' => false,
      'readonly' => false,
      'after_element_html' => '<small>Comments</small>',
      'tabindex' => 1
    ));

它的工作很好。但我想动态设置值。这意味着我想在此multiselect属性下显示所有类别列表。我搜索过很多东西。但我无法得到。我们可以使用foreach循环为该属性添加值吗?或者有其他方法可以做到这一点?请帮帮我们!

1 个答案:

答案 0 :(得分:3)

您可以先使用任何数据准备值数组,然后在表单中使用它。对于类别,它是这样的:

$categories = Mage::getModel('catalog/category');
$values = array();
foreach ($categories as $category) {
    $values[] = array('label' => $category->getName(), 'value' => $category->getId());
}

希望它清楚并适合你。