Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Can't delete mulitple select value in the product admin

I created a new attribute (multiple select) with some values, everything works fine but when I want to delete all the selected values for a product, I get the message "The product attribute has been saved." but the values are still selected.

Notes:

  • I press Ctrl + Click to unselect the last value before I save.
  • I set the parameter Value Required of my attribute to No
  • If I save a product without any value selected yet, then no values get selected
  • My Indexes are properly refreshed
  • See below two screens, on the left the parameters of my attribute and on the right my multiple select.

enter image description here

I'm running out of ideas so thanks for your help.

like image 621
adrien54 Avatar asked Feb 10 '12 10:02

adrien54


4 Answers

There is a feature called <can_be_empty> you need to go to your system.xml and add this configuration into your file:

<can_be_empty>1</can_be_empty>

then inspect the element and remove the selected="selected" and hit save, now you can save the multi-select without any values.

like image 29
Nickool Avatar answered Sep 23 '22 02:09

Nickool


Yes I found this a big pain in the bum too BUT it is an improvement on the previous bug which caused drop down attribute selections to be wiped if you tried to update attributes for several products at once.

Anyway, here is my what I do if I want to remove an option from products using a drop down attribute:

  1. Go to Manage attributes
  2. Click Manage Label Options
  3. Add a temporary option to the list
  4. Assign this new attribute option to all the products you want to change
  5. Delete the temporary attribute option

All solved.

like image 29
barella Avatar answered Sep 23 '22 02:09

barella


Add a non existent option to html via chrome/firefox developer tool, select that option and save. eg.

<option value="99999999">Click this to unselect option</option>
like image 24
Uğur Taşdildiren Avatar answered Sep 23 '22 02:09

Uğur Taşdildiren


This is a known (annoying) behaviour of the Magento Adminhtml forms.
The problem is that if no value is selected for the multiselect, no value for that attribute is posted when the form is submitted.

On the server side Magento then loads the model, sets all the posted attribute values on the model and saves it.
Because no value was posted the original value that was loaded on the model wasn't updated.

As a solution for attributes with a custom source model I tend to provide an empty option with a special option value (e.g. -1). That value must not be 0 or an empty string.

Then I specify a backend model for that attribute that checks for that special value in the _beforeSave() method. If it is found the backend model unsets the attribute on the model instance.

Here is an example:

Source Model:

class Your_Module_Model_Entity_Attribute_Source_Example
    extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    const EMPTY = '-1';

    public function getAllOptions()
        $options = array(
            array('value' => 1, 'label' => 'One'),
            array('value' => 2, 'label' => 'Two'),
            array('value' => 3, 'label' => 'Three')
        );
        if ($this->getAttribute()->getFrontendInput() === 'multiselect')
        {
            array_unshift($options, array('value' => self::EMPTY, 'label' => ''));
        }
        return $options;
    }
}

Backend Model:

class Your_Module_Model_Entity_Attribute_Backend_Example
    extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    public function beforeSave($object)
    {
        $code = $this->getAttribute()->getAttributeCode();
        $value = $object->getData($code);
        if ($value == Your_Module_Model_Entity_Attribute_Source_Example::EMPTY)
        {
            $object->unsetData($code);
        }
        return parent::beforeSave($object);
    }
}

If you find a better workaround please let me know.

like image 174
Vinai Avatar answered Sep 21 '22 02:09

Vinai