Writing to a custom checkbox field

Gossamer

Active member
I'm working on code that's supposed to add a value to a user's custom fields. They're checkbox fields. But instead of adding the value, it's replacing all of the values of that checkbox field.

It works great for my single value fields, but it overwrites existing data in the checkbox field. I figure I need to pull in the current values and add in the new one next to them. Haven't worked out how to do that yet though.

PHP:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dw->setExistingData($user);
$dw->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
 $dw->setCustomFields(array($skillfield => $selection));
 $dw->save();

And here is the full function in case it helps:
PHP:
    public static function buyAdvantage(array &$buyer, $purchaseId, array $purchaseInfo)
    {
        $userModel = XenForo_Model::create('XenForo_Model_User');  
        $categoryModel = XenForo_Model::create('DBTech_Shop_Model_Category');
        $userFieldModel = XenForo_Model::create('XenForo_Model_UserField');
      
        $user = $userModel->getUserById($buyer['user_id']);
      
        /* Retrieve Item Category Name */
        $category = $categoryModel->getCategoryById($purchaseInfo['categoryid']);
        $categoryTitle = $category['title'];
        $arr = explode(' ',trim($categoryTitle));
        $firstWord = strtolower($arr[0]);
      
        /* Retrieve Item Name, remove rank and replace spaces with _ if exists */
        $item = strtolower(preg_replace(
            $patterns = array("/\s\([^)]+\)/", "/\s/"),
            $replace = array("", "_"),
            $purchaseInfo['title']));
      
        /* Create field name from category name */
        $skillfield = "adv_" . $firstWord;
      
        /* Get Field Options */
        $field = $userFieldModel->getUserFieldById($skillfield);
        $options = $userFieldModel->getUserFieldChoices($field['field_id'],$field['field_choices']);
      
        /* Find the option we want to select */
        foreach($options as $key=>$value) {
            if($item == substr($key, 0, -1)) {
                $selection = $key;
                break;
            }
        }      
  
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        $dw->setExistingData($user);
        $dw->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
        $dw->setCustomFields(array($skillfield => $selection));
        $dw->save();
      
        return true;
    }

Thanks!
 
If you want to add a value to a checkbox/multi select field, you'd need to get the original value of the field (as an array) and then add your selection to it. You may want to examine the data structure used when a checkbox field is saved elsewhere to make sure you're replicating that.
 
Thanks! This seems to be working for me now:
PHP:
$user = $userModel->getFullUserById($buyer['user_id']);

/* Get current values of field */
        $customfields = unserialize($user['custom_fields']);
       
        /* Add new value to current values */
        $customfields[$skillfield][$selection] = $selection;
        $selection = $customfields[$skillfield];
       
        /* Set values */
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
        $dw->setExistingData($user);
        $dw->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
        $dw->setCustomFields(array($skillfield => $selection));
        $dw->save();
       
        return true;
 
Top Bottom