Xus
Xus

Reputation: 417

Magento attribute dropdown with first empty value

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:

Look at the white row

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.

Upvotes: 1

Views: 4223

Answers (1)

Xus
Xus

Reputation: 417

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

Upvotes: 3

Related Questions