Adding options to a select programatically

Permalink 3 users found helpful
I have an attribute called "Categories" that is a select with multiple options and allowed user input, and I am trying to set this value based on a CSV page import script that I am writing. For some reason, you cannot set a multiple select attribute with the standard $c->setAttribute('attrib_handle', $data) method, and I cannot seem to trace the proper way to get this done. I think SelectAttributeTypeOption and SelectAttributeTypeOptionList seem to be the right path, but I can't crack this. Has anyone dealt with this before that can shed some light? Here's what I've got for right now (not working):

$catAttrib = CollectionAttributeKey::getByHandle('project_categories');
$catOptions = new SelectAttributeTypeOptionList();
foreach ($projCats as $i => $cat) {
    $opt = new SelectAttributeTypeOption(0, $cat, $i);
    $opt->add($catAttrib, $cat);
}


Thanks for any help!

aghouseh
 
aghouseh replied on at Permalink Reply
aghouseh
Nevermind! I found it thanks to @jobb_ (https://twitter.com/jobb_)

http://pastebin.com/2SbtG5GC

Seems so simple.. ugh.
aghouseh replied on at Permalink Reply
aghouseh
Oops! This is not valid.

It appears that using this method only works if the attributeValue has been entered PREVIOUSLY. If you are trying to add a NEW value to that attribute, it fails. That took a stroke of luck to figure out, but only adds to my frustration.
moosh replied on at Permalink Reply
moosh
Hi,

Have you try this ?

$c->setAttribute('attr_handle', array(1, 2, 3, 4));
aghouseh replied on at Permalink Reply
aghouseh
Yes, that works fine if an instance of that value already exists within the attribute (i.e. if 1 has already been added). If, for example, the current select only houses values for 1-3, the attribute would only be saved with 1, 2, 3 as values and the 4 would be ignored. :(
moosh replied on at Permalink Reply
moosh
Yes,

So you want to add attribute value with api ?
aghouseh replied on at Permalink Reply
aghouseh
Correct. My intent was to generate pages from a spreadsheet that has a column named "categories" with a csv field. I was able to get around this limitations for my own usage by adding them manually (this import script was a one-time use, and the "categories" were limited to 12 or so), but I'm dying to know how to do this as I want to do some more work with multiple select attributes shortly.
moosh replied on at Permalink Reply
moosh
Try this :

$catAttrib = CollectionAttributeKey::getByHandle('project_categories');
foreach($projCats as $cat){
  SelectAttributeTypeOption::add($catAttrib, $cat);
}
aghouseh replied on at Permalink Best Answer Reply
aghouseh
This was the right path. I finally got this solved.

$ak = CollectionAttributeKey::getByHandle('tags');
$tags = array('Architecture', 'Interior', 'Kitchen', 'French', 'Light');
$tagValues = array();
foreach ($tags as $tag) {
   if (!$tagValueObject = SelectAttributeTypeOption::getByValue($tag, $ak)) {
      $tagValueObject = SelectAttributeTypeOption::add($ak, $tag);
   }
   $tagValues[] = $tagValueObject;
}
$page->setAttribute(tags', $tagValues);