I've got this
// Creating users through frontend
Event::on(Users::class, Users::EVENT_BEFORE_ACTIVATE_USER, function(UserEvent $event) {
$userId = $event->user->id;
// Allow assigning user groups through the frontend (circumvent elevated user sessions)
$groupId = Craft::$app->getRequest()->getBodyParam('userGroup');
if ($groupId) {
Craft::$app->getUsers()->assignUserToGroups($userId, [$groupId]);
}
// This does not work
Craft::$app->users->getUserById($userId)->setFieldValue('discountPercentage', 15);
});
Assigning the user group works fine, so I don't get why assigning a field value doesn't. The discountPercentage
field is a simple Number fieldtype
I also tried $event->user->setFieldValue('discountPercentage', 15);
without success.
Also I tried surrounding 15
in quotes, like '15'
, but that didn't work either.
Edit:
Submitting the "create user form" through the frontend gives no errors. discountPercentage
is just left blank.