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

CC BY-SA 3.0
2016-05-06T23:41:21.277Z
2016-05-06T23:41:21.277Z
Jeremy Daalder
<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>

<plugin-craftcommerce>
Commerce - How do I add the purchaser of a specific product to a user group?
# 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 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 ids 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]);
    }
});


---

