Answers.Body: <p>Setting the new field value <code>onSaveProduct</code> meant that the only the general element and its custom field data was being saved and not the tax ID on the product model.</p>
  
  <p>To solve this, the <code>onBeforeSaveProduct</code> event should be used. This allows modification of the product model before it's saved.</p>
  
  <pre><code>craft()-&gt;on('commerce_products.onBeforeSaveProduct', function(Event $event){
      $product = $event-&gt;params['product'];
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;id = $product-&gt;productFilterArtist[0]-&gt;id;
      $artist = $criteria-&gt;first();
      if ($artist &amp;&amp; $artist-&gt;getContent()-&gt;vatRegistered) {
          $product-&gt;taxCategoryId = 1;
      }
  })
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-16T12:27:08.753Z
Answers.LastActivityDate: 2017-01-16T12:27:08.753Z
Answers.UserDisplayName: Luke Pearce
Body: <p>I'm trying to alter a products tax category whenever it's saved based on a condition.</p>
  
  <pre><code>craft()-&gt;on('commerce_products.onSaveProduct', function(Event $event){
      $product = $event-&gt;params['product'];
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;id = $product-&gt;productFilterArtist[0]-&gt;id;
      $artist = $criteria-&gt;first();
  
      //Craft::dd($product-&gt;taxCategoryId); // 2
  
      if ($artist-&gt;getContent()-&gt;vatRegistered) {
          $product-&gt;taxCategoryId = 1;
          //Craft::dd($product-&gt;taxCategoryId); // 1
      }
  
      $success = craft()-&gt;elements-&gt;saveElement($product);
  
      //Craft::dd($product-&gt;taxCategoryId); // 1
  });
  </code></pre>
  
  <p>It appears to work as the output of each <code>Craft::dd</code> is correct. However the entry still shows the original tax category in the CP.</p>
  
  <p>I have been able to alter the shipping category in the same way without trouble. Am I missing something?</p>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: Changing a product's Tax Category ID onSaveProduct event
ThreadText: # Changing a product's Tax Category ID onSaveProduct event
  
  (Asked by: username_0 on 2017-01-12)
  
  I'm trying to alter a products tax category whenever it's saved based on a condition.
  
  ```
  craft()->on('commerce_products.onSaveProduct', function(Event $event){
      $product = $event->params['product'];
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->id = $product->productFilterArtist[0]->id;
      $artist = $criteria->first();
  
      //Craft::dd($product->taxCategoryId); // 2
  
      if ($artist->getContent()->vatRegistered) {
          $product->taxCategoryId = 1;
          //Craft::dd($product->taxCategoryId); // 1
      }
  
      $success = craft()->elements->saveElement($product);
  
      //Craft::dd($product->taxCategoryId); // 1
  });
  
  ```
  
  It appears to work as the output of each `Craft::dd` is correct. However the entry still shows the original tax category in the CP.
  
  I have been able to alter the shipping category in the same way without trouble. Am I missing something?
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-01-16. Score: 1 | Accepted answer
  
  Setting the new field value `onSaveProduct` meant that the only the general element and its custom field data was being saved and not the tax ID on the product model.
  
  To solve this, the `onBeforeSaveProduct` event should be used. This allows modification of the product model before it's saved.
  
  ```
  craft()->on('commerce_products.onBeforeSaveProduct', function(Event $event){
      $product = $event->params['product'];
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->id = $product->productFilterArtist[0]->id;
      $artist = $criteria->first();
      if ($artist && $artist->getContent()->vatRegistered) {
          $product->taxCategoryId = 1;
      }
  })
  
  ```
  
  
  ---
  
  