Answers.Body: <p>I haven't done this, but as a rule I try an mimic what Commerce actually does itself.  In this area I would be inclined to follow the code Commerce uses in it's own completed order handling, found in <code>public function orderCompleteHandler</code> in <code>commerce/services/Commerce_VariantsService.php</code> around line 290:</p>
  
  <pre><code>        $purchasable = $lineItem-&gt;getPurchasable();
  
          // Only reduce variant stock if the variant exists in db
          if (!$purchasable)
          {
              continue;
          }
  
          $clearCacheOfElementIds = [];
          if ($purchasable instanceof Commerce_VariantModel &amp;&amp; !$purchasable-&gt;unlimitedStock)
          {
  
              // Update the qty in the db
              craft()-&gt;db-&gt;createCommand()-&gt;update('commerce_variants',
                  ['stock' =&gt; new \CDbExpression('stock - :qty', [':qty' =&gt; $lineItem-&gt;qty])],
                  'id = :variantId',
                  [':variantId' =&gt; $purchasable-&gt;id]);
  
              // Update the stock
              $purchasable-&gt;stock = craft()-&gt;db-&gt;createCommand()
                  -&gt;select('stock')
                  -&gt;from('commerce_variants')
                  -&gt;where('id = :variantId', [':variantId' =&gt; $purchasable-&gt;id])
                  -&gt;queryScalar();
  
              // Clear the cache since the stock changed
              $clearCacheOfElementIds[] = $purchasable-&gt;id;
              $clearCacheOfElementIds[] = $purchasable-&gt;product-&gt;id;
          }
  
          $clearCacheOfElementIds = array_unique($clearCacheOfElementIds);
          craft()-&gt;templateCache-&gt;deleteCachesByElementId($clearCacheOfElementIds);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-24T01:37:14.357Z
Answers.LastActivityDate: 2017-05-24T01:37:14.357Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>Has anyone had any luck trying to save a variant via a plugin? </p>
  
  <p>I'm trying to integrate with a 3rd party inventory system and want to grab the current stock information and update my variants. Currently I have</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Commerce_Variant');
  $criteria-&gt;sku = $sku;
  $variant = $criteria-&gt;first();
  </code></pre>
  
  <p>Which works just fine to grab the variant i need, but...</p>
  
  <pre><code>$variant-&gt;stock = 99;
  craft()-&gt;commerce_variants-&gt;saveVariant($variant);
  </code></pre>
  
  <p>This gives me no error, but does not update the variant in the database. What is the method I should be using to achieve this?</p>
  
Tags: <plugin-development><plugin-craftcommerce><ecommerce><stock>
Title: Saving a variant with new stock from plugin
ThreadText: # Saving a variant with new stock from plugin
  
  (Asked by: username_0 on 2017-05-23)
  
  Has anyone had any luck trying to save a variant via a plugin?
  
  I'm trying to integrate with a 3rd party inventory system and want to grab the current stock information and update my variants. Currently I have
  
  ```
  $criteria = craft()->elements->getCriteria('Commerce_Variant');
  $criteria->sku = $sku;
  $variant = $criteria->first();
  
  ```
  
  Which works just fine to grab the variant i need, but...
  
  ```
  $variant->stock = 99;
  craft()->commerce_variants->saveVariant($variant);
  
  ```
  
  This gives me no error, but does not update the variant in the database. What is the method I should be using to achieve this?
  
  **Tags:** plugin-development, plugin-craftcommerce, ecommerce, stock
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-24. Score: 1
  
  I haven't done this, but as a rule I try an mimic what Commerce actually does itself. In this area I would be inclined to follow the code Commerce uses in it's own completed order handling, found in `public function orderCompleteHandler` in `commerce/services/Commerce_VariantsService.php` around line 290:
  
  ```
          $purchasable = $lineItem->getPurchasable();
  
          // Only reduce variant stock if the variant exists in db
          if (!$purchasable)
          {
              continue;
          }
  
          $clearCacheOfElementIds = [];
          if ($purchasable instanceof Commerce_VariantModel && !$purchasable->unlimitedStock)
          {
  
              // Update the qty in the db
              craft()->db->createCommand()->update('commerce_variants',
                  ['stock' => new \CDbExpression('stock - :qty', [':qty' => $lineItem->qty])],
                  'id = :variantId',
                  [':variantId' => $purchasable->id]);
  
              // Update the stock
              $purchasable->stock = craft()->db->createCommand()
                  ->select('stock')
                  ->from('commerce_variants')
                  ->where('id = :variantId', [':variantId' => $purchasable->id])
                  ->queryScalar();
  
              // Clear the cache since the stock changed
              $clearCacheOfElementIds[] = $purchasable->id;
              $clearCacheOfElementIds[] = $purchasable->product->id;
          }
  
          $clearCacheOfElementIds = array_unique($clearCacheOfElementIds);
          craft()->templateCache->deleteCachesByElementId($clearCacheOfElementIds);
  
  ```
  
  
  ---
  
  