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

5

5

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.

Swift answered 10/2, 2012 at 10:59 Comment(0)
H
11

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.

Hopeh answered 10/2, 2012 at 11:35 Comment(0)
H
3

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.

Heman answered 22/11, 2016 at 19:31 Comment(0)
I
2

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>
Incomprehensible answered 16/2, 2017 at 14:39 Comment(0)
M
1

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.

Myramyrah answered 19/6, 2013 at 11:30 Comment(0)
R
0

Just ran into this problem in Magento 1.7.0.2, my solution :

  • Use Firefox with Firebug

  • right-click the multiselect list, choose Inspect with Element and you'll see something like this at the bottom in Firebug : XLarge

  • Double-click on selected, right-click, cut, no more selected attribute and just save the page.

Rippy answered 18/2, 2013 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.