Magento attribute dropdown with first empty value

天涯浪子 提交于 2019-12-11 12:12:54

问题


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:

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" } } 

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


回答1:


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

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

The $withEmpty adds an empty option to array

Just pass false to getAllOptions().

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


来源:https://stackoverflow.com/questions/31534910/magento-attribute-dropdown-with-first-empty-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!