Answers.Body: <p>Create a business logic plugin if you haven't already.  </p>
  
  <p>Two good ways of doing that are : <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow">Business Logic Plugin</a> or via <a href="http://pluginfactory.io" rel="nofollow">http://pluginfactory.io</a></p>
  
  <p>Then, in your plugin's main file in the <code>init</code> method, register a listener method:</p>
  
  <pre><code>public function init()
  {      
      craft()-&gt;on('commerce_variants.onOrderVariant', 
          [
              craft()-&gt;businessLogic_logic,
              "onOrderVariantHandler"
          ]
      );
  }
  </code></pre>
  
  <p>Then, implement the method (I do this in my class BusinessLogic_Logic Service):</p>
  
  <pre><code>public function onOrderVariantHandler($event){
  
      $variant = $event-&gt;params['variant'];
      if ($variant-&gt;product-&gt;id == "4194" ){
          $user = craft()-&gt;userSession-&gt;getUser();
          craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, [3]);
      }
  
  }
  </code></pre>
  
  <p>Obviously make sure you use the right <code>id</code>s for your particular products &amp; groups!</p>
  
  <p>Note you can also just implement the method directly in <code>init()</code> if you want to keep things very simple - but the above method makes for a cleaner main plugin file and once you start doing a lot of this sort of thing (which is where a lot of the real power of Commerce lies I think) - separating the logic into a service and just registering the handlers is a more ordered way to go I think.</p>
  
  <pre><code>craft()-&gt;on('commerce_variants.onOrderVariant', function($event){
      $variant = $event-&gt;params['variant'];
      if ($variant-&gt;product-&gt;id == "4194" ){
          $user = craft()-&gt;userSession-&gt;getUser();
          craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, [3]);
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-06T23:41:21.277Z
Answers.LastActivityDate: 2016-05-06T23:41:21.277Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I want all buyers of a specific product to automatically be added to a specific user group.  </p>
  
  <p>How do I do this?</p>
  
Tags: <plugin-craftcommerce>
Title: Commerce - How do I add the purchaser of a specific product to a user group?
ThreadText: # Commerce - How do I add the purchaser of a specific product to a user group?
  
  (Asked by: username_0 on 2016-05-06)
  
  I want all buyers of a specific product to automatically be added to a specific user group.
  
  How do I do this?
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-05-06. Score: 2 | Accepted answer
  
  Create a business logic plugin if you haven't already.
  
  Two good ways of doing that are : [Business Logic Plugin](https://github.com/lindseydiloreto/craft-businesslogic) or via <http://pluginfactory.io>
  
  Then, in your plugin's main file in the `init` method, register a listener method:
  
  ```
  public function init()
  {      
      craft()->on('commerce_variants.onOrderVariant', 
          [
              craft()->businessLogic_logic,
              "onOrderVariantHandler"
          ]
      );
  }
  
  ```
  
  Then, implement the method (I do this in my class BusinessLogic\_Logic Service):
  
  ```
  public function onOrderVariantHandler($event){
  
      $variant = $event->params['variant'];
      if ($variant->product->id == "4194" ){
          $user = craft()->userSession->getUser();
          craft()->userGroups->assignUserToGroups($user->id, [3]);
      }
  
  }
  
  ```
  
  Obviously make sure you use the right `id`s for your particular products & groups!
  
  Note you can also just implement the method directly in `init()` if you want to keep things very simple - but the above method makes for a cleaner main plugin file and once you start doing a lot of this sort of thing (which is where a lot of the real power of Commerce lies I think) - separating the logic into a service and just registering the handlers is a more ordered way to go I think.
  
  ```
  craft()->on('commerce_variants.onOrderVariant', function($event){
      $variant = $event->params['variant'];
      if ($variant->product->id == "4194" ){
          $user = craft()->userSession->getUser();
          craft()->userGroups->assignUserToGroups($user->id, [3]);
      }
  });
  
  ```
  
  
  ---
  
  