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

From aMember Pro Documentation
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
Add this code to [[site.php file]]:
 
Add this code to [[site.php file]]:
 
<source>
 
<source>
<?php
+
Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_CHANGED, function (Am_Event_SubscriptionChanged $event){
 
+
$di = Am_Di::getInstance();
+
$di->hook->add(Am_Event::SUBSCRIPTION_CHANGED, 'assignUsergroupOnPurchase');
+
 
+
function assignUsergroupOnPurchase(Am_Event_SubscriptionChanged $event)
+
{
+
 
     // configuration
 
     // configuration
 
     $products = array(3,4,5); // product# that will trigger group addition
 
     $products = array(3,4,5); // product# that will trigger group addition
Line 31: Line 25:
 
         //}
 
         //}
 
     }
 
     }
}
+
});
  
 
</source>
 
</source>

Latest revision as of 05:05, 5 February 2016

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