Difference between revisions of "How to assign a usergroup when customers orders a product"

From aMember Pro Documentation
Jump to: navigation, search
(Created page with "Add this code to /amember/application/configs/site.php <source> <?php $di = Am_Di::getInstance(); $di->hook->add(Am_Event::SUBSCRIPTION_CHANGED, 'assignUsergroupOnPurchase'); ...")
 
Line 11: Line 11:
 
     $products = array(3,4,5); // product# that will trigger group addition
 
     $products = array(3,4,5); // product# that will trigger group addition
 
     $group_id = 11; // user group# that will be added on purchase of products
 
     $group_id = 11; // user group# that will be added on purchase of products
 +
 
     // the action
 
     // the action
 
     $user = $event->getUser();   
 
     $user = $event->getUser();   

Revision as of 04:33, 25 March 2013

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);
        //}
    }
}