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.
And here is the full function in case it helps:
Thanks!
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!