<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>

CC BY-SA 3.0
2016-11-17T07:23:30.840Z
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?
taylor
CC BY-SA 3.0
2016-11-17T07:26:55.967Z
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.
taylor
CC BY-SA 3.0
2016-11-17T08:22:53.873Z
No worries.  Hoping you're going to build us all a totally sweet product editing system!
Jeremy Daalder
CC BY-SA 3.0
2016-11-17T04:30:50.947Z
2016-11-17T05:01:31.670Z
2016-11-17T05:01:31.670Z
Jeremy Daalder
<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>

CC BY-SA 3.0
2016-11-17T05:02:01.267Z
More proper answer now below...
Jeremy Daalder
<plugin-development><plugin-craftcommerce><elementcriteriamodel><php>
Craft Commerce update variant field in plugin
# 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 Jeremy.  
> **username_1**: No worries.  Hoping you're going to build us all a totally sweet product editing system!  

---

