Answers.Body: <p>This is the general mechanism for updating custom fields on elements - use <code>setContentFromPost</code> and then call the element's save function.</p>
  
  <pre><code>    $variant-&gt;setContentFromPost(array(
          'fieldHandle' =&gt; $newValue,
      ));
      craft()-&gt;commerce_variants-&gt;saveVariant($variant);
  </code></pre>
  
  <p>You should catch exceptions around that really, to handle any errors that may arise (as the model gets validated during save etc.)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-17T07:23:30.840Z
Answers.Text: I'm running into the same type of error I keep getting for trying to access fields etc. In this case it is: 'Call to a member function setContentFromPost() on a non-object' Am I going about retrieving the variant models the wrong way for it to not be returning an object?
Answers.UserDisplayName: taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-17T07:26:55.967Z
Answers.Text: Oops, I was getting an array returned through ->find(), changed to ->first(). Now your suggestion works! I'll be sure to catch exceptions. Super appreciate the help Jeremy.
Answers.UserDisplayName: taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-17T08:22:53.873Z
Answers.Text: No worries.  Hoping you're going to build us all a totally sweet product editing system!
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-17T04:30:50.947Z
Answers.LastActivityDate: 2016-11-17T05:01:31.670Z
Answers.LastEditDate: 2016-11-17T05:01:31.670Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I'm trying to get, update, and save products &amp;/or variants in my plugin controller, and am stuck on accessing/changing/saving updated fields.</p>
  
  <pre><code>// get product ID from post
  $variant_id = craft()-&gt;request-&gt;getPost('variant_id');
  
  // create criteria
  $criteria = craft()-&gt;elements-&gt;getCriteria('Commerce_Variant');
  $criteria-&gt;id = $variant_id;
  
  // query
  $variant = $criteria-&gt;first();
  </code></pre>
  
  <p>The above works just fine and gets the variant. But now I need to update a few fields and save it. Any idea how to do this?</p>
  
  <pre><code>$variant-&gt;getContent()-&gt;myCustomField = 'new value';
  </code></pre>
  
  <p>How would I then save <code>$variant</code>?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-17T05:02:01.267Z
Comments.Text: More proper answer now below...
Comments.UserDisplayName: Jeremy Daalder
Tags: <plugin-development><plugin-craftcommerce><elementcriteriamodel><php>
Title: Craft Commerce update variant field in plugin
ThreadText: # Craft Commerce update variant field in plugin
  
  (Asked by: username_0 on 2016-11-17)
  
  I'm trying to get, update, and save products &/or variants in my plugin controller, and am stuck on accessing/changing/saving updated fields.
  
  ```
  // get product ID from post
  $variant_id = craft()->request->getPost('variant_id');
  
  // create criteria
  $criteria = craft()->elements->getCriteria('Commerce_Variant');
  $criteria->id = $variant_id;
  
  // query
  $variant = $criteria->first();
  
  ```
  
  The above works just fine and gets the variant. But now I need to update a few fields and save it. Any idea how to do this?
  
  ```
  $variant->getContent()->myCustomField = 'new value';
  
  ```
  
  How would I then save `$variant`?
  
  **Tags:** plugin-development, plugin-craftcommerce, elementcriteriamodel, php
  
  > **Comments:**
  > **username_1**: More proper answer now below...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-17. Score: 4 | Accepted answer
  
  This is the general mechanism for updating custom fields on elements - use `setContentFromPost` and then call the element's save function.
  
  ```
      $variant->setContentFromPost(array(
          'fieldHandle' => $newValue,
      ));
      craft()->commerce_variants->saveVariant($variant);
  
  ```
  
  You should catch exceptions around that really, to handle any errors that may arise (as the model gets validated during save etc.)
  
  > **Comments:**
  > **username_0**: I'm running into the same type of error I keep getting for trying to access fields etc. In this case it is: 'Call to a member function setContentFromPost() on a non-object' Am I going about retrieving the variant models the wrong way for it to not be returning an object?  
  > **username_0**: Oops, I was getting an array returned through ->find(), changed to ->first(). Now your suggestion works! I'll be sure to catch exceptions. Super appreciate the help username_1.  
  > **username_1**: No worries.  Hoping you're going to build us all a totally sweet product editing system!  
  
  ---
  
  