Answers.Body: <p>Have figured it out. Need to:</p>
  <ul>
  <li>use craft\commerce\elements\Variant; // not Product</li>
  <li>setFieldValue still does not find variable stock with Variant, but set it using $variantEntry-&gt;stock = 20;</li>
  <li>Need to find() on Variant to return the actual variant, and not
  find product with variant as that returns the product</li>
  </ul>
  <p>Following works, changed $productEntry to $variantEntry</p>
  <pre><code>use Craft;
  use craft\web\Controller;
  use craft\helpers\ElementHelper;
  use craft\commerce\elements\Variant;
  
  $variantEntry = Variant::find()-&gt;sku('mysku')-&gt;first();
  if($variantEntry)
  {
      if (!$variantEntry-&gt;hasUnlimitedStock)
      {
          $variantEntry-&gt;stock = 20;
          echo Craft::$app-&gt;elements-&gt;saveElement($variantEntry);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-20T10:27:17.680Z
Answers.LastActivityDate: 2021-01-20T10:27:17.680Z
Answers.UserDisplayName: Laurence Cope
Body: <p>I am trying to update stock of product variants using a custom plugin on our Craft 3 eCommerce site (its a sync process with an external database). But I can't figure out how to update stock, I get an unknown property error for the field stock.</p>
  <pre><code>use Craft;
  use craft\web\Controller;
  use craft\helpers\ElementHelper;
  use craft\commerce\elements\Product;
  
  $productEntry = Product::find()-&gt;hasVariant(['sku' =&gt; 'mysku'])-&gt;first();
  if($productEntry)
  {
      if (!$productEntry-&gt;hasUnlimitedStock)
      {
          $productEntry-&gt;setFieldValue('stock', '10');
          echo Craft::$app-&gt;elements-&gt;saveElement($productEntry);
      }
  }
  </code></pre>
  <p>But I get a &quot;Setting unknown property: craft\behaviors\CustomFieldBehavior::stock&quot; error:-</p>
  <pre><code>2021-01-18 11:21:33 [-][1][9319132c27bbf8aa77f29e2606c2b4ea][error][yii\base\UnknownPropertyException] yii\base\UnknownPropertyException: Setting unknown property: craft\behaviors\CustomFieldBehavior::stock in /home/mysite/craftcms/vendor/yiisoft/yii2/base/BaseObject.php:163
  Stack trace:
  #0 /home/mysite/craftcms/storage/runtime/compiled_classes/CustomFieldBehavior_zmbofhbcdvie.php(895): yii\base\BaseObject-&gt;__set('stock', '10')
  #1 /home/mysite/craftcms/vendor/craftcms/cms/src/base/Element.php(2672): craft\behaviors\CustomFieldBehavior-&gt;__set('stock', '10')
  #2 /home/mysite/craftcms/plugins/mysite/src/controllers/SyncController.php(225): craft\base\Element-&gt;setFieldValue('stock', '10')
  #3 /home/mysite/craftcms/plugins/mysite/src/controllers/SyncController.php(90): mysite\mysite\controllers\SyncController-&gt;updateEntries(Array)
  #4 [internal function]: mysite\mysite\controllers\SyncController-&gt;actionSyncStock()
  #5 /home/mysite/craftcms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
  #6 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction-&gt;runWithParams(Array)
  #7 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller-&gt;runAction('sync-stock', Array)
  #8 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller-&gt;runAction('sync-stock', Array)
  #9 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module-&gt;runAction('mysite/sync/sy...', Array)
  #10 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(577): craft\web\Application-&gt;runAction('mysite/sync/sy...', Array)
  #11 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(253): craft\web\Application-&gt;_processActionRequest(Object(craft\web\Request))
  #12 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application-&gt;handleRequest(Object(craft\web\Request))
  #13 /home/mysite/public_html/index.php(23): yii\base\Application-&gt;run()
  #14 {main}
  2021-01-18 11:21:32 [-][1][9319132c27bbf8aa77f29e2606c2b4ea][info][application] $_GET = [
      'p' =&gt; 'actions/mysite/sync/sync-stock'
  ]
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-20T10:14:32.657Z
Comments.Text: Also added the following but same error:
  use craft\commerce\elements\Variant;
Comments.UserDisplayName: Laurence Cope
Tags: <craft3><saveentry><element>
Title: How to update stock on a product variant? Setting unknown property error
ThreadText: # How to update stock on a product variant? Setting unknown property error
  
  (Asked by: username_0 on 2021-01-18)
  
  I am trying to update stock of product variants using a custom plugin on our Craft 3 eCommerce site (its a sync process with an external database). But I can't figure out how to update stock, I get an unknown property error for the field stock.
  
  ```
  use Craft;
  use craft\web\Controller;
  use craft\helpers\ElementHelper;
  use craft\commerce\elements\Product;
  
  $productEntry = Product::find()->hasVariant(['sku' => 'mysku'])->first();
  if($productEntry)
  {
      if (!$productEntry->hasUnlimitedStock)
      {
          $productEntry->setFieldValue('stock', '10');
          echo Craft::$app->elements->saveElement($productEntry);
      }
  }
  
  ```
  
  But I get a "Setting unknown property: craft\behaviors\CustomFieldBehavior::stock" error:-
  
  ```
  2021-01-18 11:21:33 [-][1][9319132c27bbf8aa77f29e2606c2b4ea][error][yii\base\UnknownPropertyException] yii\base\UnknownPropertyException: Setting unknown property: craft\behaviors\CustomFieldBehavior::stock in /home/mysite/craftcms/vendor/yiisoft/yii2/base/BaseObject.php:163
  Stack trace:
  #0 /home/mysite/craftcms/storage/runtime/compiled_classes/CustomFieldBehavior_zmbofhbcdvie.php(895): yii\base\BaseObject->__set('stock', '10')
  #1 /home/mysite/craftcms/vendor/craftcms/cms/src/base/Element.php(2672): craft\behaviors\CustomFieldBehavior->__set('stock', '10')
  #2 /home/mysite/craftcms/plugins/mysite/src/controllers/SyncController.php(225): craft\base\Element->setFieldValue('stock', '10')
  #3 /home/mysite/craftcms/plugins/mysite/src/controllers/SyncController.php(90): mysite\mysite\controllers\SyncController->updateEntries(Array)
  #4 [internal function]: mysite\mysite\controllers\SyncController->actionSyncStock()
  #5 /home/mysite/craftcms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
  #6 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
  #7 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller->runAction('sync-stock', Array)
  #8 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('sync-stock', Array)
  #9 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module->runAction('mysite/sync/sy...', Array)
  #10 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(577): craft\web\Application->runAction('mysite/sync/sy...', Array)
  #11 /home/mysite/craftcms/vendor/craftcms/cms/src/web/Application.php(253): craft\web\Application->_processActionRequest(Object(craft\web\Request))
  #12 /home/mysite/craftcms/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
  #13 /home/mysite/public_html/index.php(23): yii\base\Application->run()
  #14 {main}
  2021-01-18 11:21:32 [-][1][9319132c27bbf8aa77f29e2606c2b4ea][info][application] $_GET = [
      'p' => 'actions/mysite/sync/sync-stock'
  ]
  
  ```
  
  **Tags:** craft3, saveentry, element
  
  > **Comments:**
  > **username_0**: Also added the following but same error: use craft\commerce\elements\Variant;  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-01-20. Score: 1
  
  Have figured it out. Need to:
  
  * use craft\commerce\elements\Variant; // not Product
  * setFieldValue still does not find variable stock with Variant, but set it using $variantEntry->stock = 20;
  * Need to find() on Variant to return the actual variant, and not
    find product with variant as that returns the product
  
  Following works, changed $productEntry to $variantEntry
  
  ```
  use Craft;
  use craft\web\Controller;
  use craft\helpers\ElementHelper;
  use craft\commerce\elements\Variant;
  
  $variantEntry = Variant::find()->sku('mysku')->first();
  if($variantEntry)
  {
      if (!$variantEntry->hasUnlimitedStock)
      {
          $variantEntry->stock = 20;
          echo Craft::$app->elements->saveElement($variantEntry);
      }
  }
  
  ```
  
  
  ---
  
  