magento2: Unable to unselect all multiselect values

Magento version: 2.1.2

It is not possible to unselect and save all multiselect attribute values for a product. After the save completes the state of the product attribute before the product was saved is unchanged. It seems the problem is that the JS does not submit multiselect form elements with no selected values, at least on the server side completely unselected multiselect attribtues are not part of the $_POST['product'] array.

The exact same issue existed for years on M1 but was finally fixed there. It seems like this issue was introduced again for M2.

Preconditions

None special.

Steps to reproduce

  1. Create a custom multiselect attribute. Ot just install the sample data (e.g. the issue can be reproduced using the features_bag or strap_bags attributes).
  2. Open the product in the admin interface.
  3. Select one or more values of the multiselect attribtue.
  4. Save
  5. Unselect all values of the multiselect attribute
  6. Save again

Expected result

The product attribute should have no value for the product.

Actual result

The previously selected product attributes still are selected.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 10
  • Comments: 35 (14 by maintainers)

Commits related to this issue

Most upvoted comments

This issue still persist in Magento ver. 2.2.2

Still this issue also persist at magento 2.2.4 .

Have done with Vinai describe scenario and same issue is happening

@Vinai ,Is this issue resolved at your end?

@magento/community-engineering-team ,Can you please check this issue again

I had the same issue because of multiselect having an empty value (ie. equal to ‘’ or 0 or null) :

<select multiple="" class="admin__control-multiselect" data-bind="
    attr: {
        name: inputName,
        id: uid,
        size: size ? size : '6',
        disabled: disabled,
        'aria-describedby': noticeId,
        placeholder: placeholder
    },
    hasFocus: focused,
    optgroup: options,
    selectedOptions: value,
    optionsValue: 'value',
    optionsText: 'label'" name="product[customer_group]" id="MEAKNUP" size="6" aria-describedby="notice-MEAKNUP">
   <option data-title="NOT LOGGED IN" value="0">NOT LOGGED IN</option> <!-- here is the devil -->
   <option data-title="General" value="1">General</option>
   <option data-title="Conseillère" value="2">Conseillère</option>
</select>

Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter prepare your attributes before saving it.

public function prepareProductAttributes(Product $product, array $productData, array $useDefaults)
{
    foreach ($productData as $attribute => $value) {
        $considerUseDefaultsAttribute = !isset($useDefaults[$attribute]) || $useDefaults[$attribute] === "1";
        if ($value === '' && $considerUseDefaultsAttribute) {
            /** @var $product Product */
            if ((bool)$product->getData($attribute) === (bool)$value) {
                unset($productData[$attribute]);
            }
        }
    }
    return $productData;
}

When a posted value is equal to empty string (‘’) it will compare it to the product value before saving. The issue is that Magento cast both values in boolean. And when the previously selected product value was set to 0 (like “NOT LOGGED IN” in our example) it compares an empty string casted in boolean (= false) with a 0 casted in boolean (= false too) so the value is simply unset … so the attribute isn’t saved and there is no error.

In our specific case, we fixed it using a plugin to restore the value after the prepareProductAttributes function has been called.

I hope it will help.

Still present in Magento ver. 2.2.3

I’d like to know how to get this fixed. It’s driving me nuts. (Tested on both 2.1.2 and 2.1.6)