0

I am working on a magento extension that automatically creates products from an import file. Everything is going great, except for one small issue i can't seem to figure out.

I have created a few multiselect attributes for all products, but my setData function does not process multiple values for those attributes. Assigning 1 value is no problem though.

my current code (simplified):

$magentoProductModel->setData('multiselect_attribute', array(1, 3, etc));
$magentoProductModel->save();

This doesn't do anything. When i assign only 1 id like this:

$magentoProductModel->setData('multiselect_attribute', 1);
$magentoProductModel->save();

It will assign the correct value.

But i really want to assign more than 1 value. It isn't a multiselect attribute for nothing.

Methods i have tried so far:

$magentoProductModel->setData('multiselect_attribute', array(1, 3, etc));
$magentoProductModel->save();

$magentoProductModel->setData('multiselect_attribute', [1, 3, etc]);
$magentoProductModel->save();

$multiselect_ids = array(1, 3, etc); 
foreach($multiselect_ids as $multiselect_id) {
  $magentoProductModel->setMultiselectAttribute($multiselect_id);
}
$magentoProductModel->save();

None of the above methods work. Can't seem to figure out the right way to fix this. Does anyone know how to get this to work?

2

try with $magentoProductModel->setData('multiselect_attribute', '1,2,3');

I mean, concatenate the values using comma.

3
  • Thanks! So simple, but so far away. Frustrating that it isn't clear when you can insert an array of values and when not. Is there any logic to assert when an array can be used and when it has to be a string like you suggested? Or is trial and error the only way to figure these things out? – Tijs van Raaij Aug 1 '16 at 13:13
  • @Marius have you detected why is it like this? I have similar issue - when set values for multiselect attribute array of values I need to save it twice (sic!) in order to persist changes. Only your way works without doubling save operations. – Bartosz Kubicki Dec 22 '16 at 16:11
  • multiselect attribute values are stored in the table _text as comma concatentated. That's why. – Marius Dec 22 '16 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.