Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-11T09:31:46.637Z
Answers.Text: Ah so simple! Thanks!
Answers.UserDisplayName: supazu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-11T07:53:12.207Z
Answers.LastActivityDate: 2022-04-11T07:53:12.207Z
Answers.UserDisplayName: MoritzLost
Body: <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: <tags>
Title: Add a tag to a user after EVENT_AFTER_COMPLETE_ORDER
ThreadText: # 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](https://craftcms.stackexchange.com/a/26648/11495) 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](https://docs.craftcms.com/api/v3/craft-services-elements.html):
  
  ```php
  /** @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!  
  
  ---
  
  