Magento属性下拉列表,第一个空值 [英] Magento attribute dropdown with first empty value

查看:103
本文介绍了Magento属性下拉列表,第一个空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个带有product属性值的下拉列表.但总是显示第一个空位.我有2个值,但我不知道为什么数组有3个位置

I want to show one dropdown with the values from product attribute. But always is showing the first position empty. I have 2 values but I don't know why the array have 3 positions

<?php
    $options  = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'tipo_paquete')->getSource()->getAllOptions();
    var_dump($options);
?>
<select id="tipo_paquete" class="required select" name="tipo_paquete">
   <option value=""><?php echo $helper->__('--Please Select--')?></option>
   <?php
        foreach ($options as $option)
        {
           echo "<option value='".$option['value']."'>". $option['label'] ."</option>";
        }
   ?>
</select>

此代码显示了这样的选择:

This code show the select like this:

然后var_dump显示以下内容:

And the var_dump show this:

array(3) { [0]=> array(2) { ["label"]=> string(0) "" ["value"]=> string(0) "" } [1]=> array(2) { ["value"]=> string(1) "8" ["label"]=> string(15) "Caja de cartón" } [2]=> array(2) { ["value"]=> string(1) "7" ["label"]=> string(14) "Caja de madera" } } 

我不知道为什么我有3个职位,我只保存了2个选项.我对其他具有相同问题的属性进行了测试.

I don't know why I have 3 positions, I only saved 2 options. I tested with other attributes with the same problem.

推荐答案

我已经找到解决方案

I've found the solution here. getAllOptions can recieve two parameters:

array getAllOptions ([bool $withEmpty = true], [bool $defaultValues = false])

$withEmpty向数组添加一个空选项

The $withEmpty adds an empty option to array

只需将false传递给getAllOptions().

$options  = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'tipo_paquete')->getSource()->getAllOptions(false);

这篇关于Magento属性下拉列表,第一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆