<p>The syntax is correct, but you're not saving the user. Whenever you change an element, you need to save it in order to persist the changes to the database.
The only exception is if you're modifying an element during an <code>EVENT_BEFORE_SAVE</code> or similar event, you might not have to do this since the element will be saved after your code runs anyway.</p>
<p>You can save elements using the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html" rel="nofollow noreferrer"><code>craft\services\Elements</code> service</a>:</p>
<pre class="lang-php prettyprint-override"><code>/** @var craft\servicesElements $elementService */
$elementService = \Craft::$app-&gt;getElements();

$success = $elementService-&gt;saveElement($user);
if (!$success) {
    // saving failed due to a validation error or other error
}
</code></pre>

CC BY-SA 4.0
2022-04-11T09:31:46.637Z
Ah so simple! Thanks!
supazu
CC BY-SA 4.0
2022-04-11T07:53:12.207Z
2022-04-11T07:53:12.207Z
MoritzLost
<p>I'm trying to add a tag to a user to match a product that they purchase. Following <a href="https://craftcms.stackexchange.com/a/26648/11495">this answer</a> I get a complaint that the tag already exists, so instead I was thinking I could just find the tag and add it like this?</p>
<pre><code>// the tag will match the product title
$productTitle = $product-&gt;title;

$tag = Tag::find()-&gt;groupId(1)-&gt;title($productTitle)-&gt;one();
$user-&gt;setFieldValues([
    'userFieldHandle' =&gt; [$tag-&gt;id]
]);
</code></pre>
<p>I don't get any errors but it also isn't adding the tag. Would appreciate some tips!</p>

<tags>
Add a tag to a user after EVENT_AFTER_COMPLETE_ORDER
# Add a tag to a user after EVENT_AFTER_COMPLETE_ORDER

(Asked by: username_0 on 2022-04-11)

I'm trying to add a tag to a user to match a product that they purchase. Following this answer I get a complaint that the tag already exists, so instead I was thinking I could just find the tag and add it like this?
// the tag will match the product title
$productTitle = $product->title;

$tag = Tag::find()->groupId(1)->title($productTitle)->one();
$user->setFieldValues([
    'userFieldHandle' => [$tag->id]
]);

I don't get any errors but it also isn't adding the tag. Would appreciate some tips!

**Tags:** tags

---

## 1 answers

### Answer by username_1 on 2022-04-11. Score: 2 | Accepted answer

The syntax is correct, but you're not saving the user. Whenever you change an element, you need to save it in order to persist the changes to the database.
The only exception is if you're modifying an element during an EVENT_BEFORE_SAVE or similar event, you might not have to do this since the element will be saved after your code runs anyway.
You can save elements using the craft\services\Elements service:
/** @var craft\servicesElements $elementService */
$elementService = \Craft::$app->getElements();

$success = $elementService->saveElement($user);
if (!$success) {
    // saving failed due to a validation error or other error
}

> **Comments:**
> **username_0**: Ah so simple! Thanks!  

---

