How to trigger custom code when a user is granted access to a specific product

Discussion in 'Customization & add-ons' started by alphamr, Nov 17, 2017.

  1. alphamr

    alphamr aMember Pro Customer

    Joined:
    Feb 17, 2015
    Messages:
    2
    Hi.
    Forgive the maybe trivial question, but I don't know how to trigger custom PHP code when a user is granted access to a specific product, both manually and after regular payment.
    I've tried by hooking directly into wordpress API (wordpress' add_user_role) but it doesn't work. Apparently aMember is manipulating wordpress' database directly, bypassing it's hooks.
    How do I circumvent it?
    Thanks
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,689
    Hello,

    You can use the following code in site.php file:
    http://www.amember.com/docs/Site.php_file

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::ACCESS_AFTER_INSERT, function(Am_Event $e) {
        
    $access $e->getAccess();
        if (
    $access->product_id == 1) {
            
    /* @var $user User */
            
    $user $access->getUser();
            
    //do some stuff for product with ID 1
        
    }
    });
    Reference:
    http://www.amember.com/docs/API/HookManager

    Best Regards.
  3. alphamr

    alphamr aMember Pro Customer

    Joined:
    Feb 17, 2015
    Messages:
    2
    Thanks Caesar. I've found the solution. The event USER_AFTER_INSERT or USER_AFTER_UPDATE are triggered only upon manipulation of user's profile page. I used SUBSCRIPTION_ADDED instead, and it works, thanks.

Share This Page