How to assign a usergroup when customers orders a product

From aMember Pro Documentation
Revision as of 04:33, 25 March 2013 by Alex-scott (Talk | contribs)

Jump to: navigation, search

Add this code to /amember/application/configs/site.php

<?php
 
$di = Am_Di::getInstance();
$di->hook->add(Am_Event::SUBSCRIPTION_CHANGED, 'assignUsergroupOnPurchase');
 
function assignUsergroupOnPurchase(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);
        //}
    }
}