How to assign a usergroup when customers orders a product

From aMember Pro Documentation
Jump to: navigation, search

Add this code to site.php file:

Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_CHANGED, function (Am_Event_SubscriptionChanged $event){
    // configuration
    $products = array(3,4,5); // product# that will trigger group addition
    $group_id = 11; // user group# that will be added on purchase of products
 
    // the action
    $user = $event->getUser();   
    if (array_intersect($products, $user->getActiveProductIds())) // if user has subscription to one of $products
    {
        $groups = $user->getGroups();
        if (!in_array($group_id, $groups))
        {
            $groups[] = $group_id;
            $user->setGroups($groups);
        }
    } else {
        // uncomment the following 6 lines (delete //) to REMOVE usergroup when subscription expires
        //$groups = $user->getGroups();
        //if (in_array($group_id, $groups))
        //{
        //    array_remove_value($groups, $group_id);
        //    $user->setGroups($groups);
        //}
    }
});