Custom plugin to watches for new member signups

Discussion in 'aMember Pro v.4' started by maximize, Mar 7, 2012.

  1. maximize

    maximize Member

    Joined:
    Mar 5, 2007
    Messages:
    108
    I'm trying to create a custom plugin that watches for new users. Here's what I have so far:

    PHP:
    class Am_Plugin_Mywatcher extends Am_Plugin
    {
        const 
    PLUGIN_STATUS self::STATUS_PRODUCTION;
        const 
    PLUGIN_REVISION '1.0.0';

        function 
    onSignupUserAdded(Am_Event $event)
        {
            
    $user $event->getUser();

            @
    mail('user@example.com''aMember4 new user'print_r($userTRUE));
        }
    }
    I haven't been able to find a good resource that lists all the hooks available. I'm not even sure that onSignupUserAdded is going to do anything for me, or do it for me when I need it to. So far, this isn't sending me an email (my email address excluded from the example on purpose).

    Any ideas?

    Thanks!
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Try this:
    function onSignupUserAdded(Am_Event $event)
    {
    $user = $event->getUser();
    $this->getDi()->errorLogTable->log("SIGNUP USER ADDED:" . print_r($user->toArray(), true));
    }



    This hook is called in signup process immediately after user creation (during testing use new username to be sure).

    You may find list of all hooks inside file library/Am/Event.php
  3. maximize

    maximize Member

    Joined:
    Mar 5, 2007
    Messages:
    108
    I'm not sure I see how that's different than my original code except it looks like you're sending it to a table in the database?

    I need access the a user's data and the product they've signed up for, whether through the public side of the website or added manually in the admin. I believe in v3 there were specific hooks for this...

    xxxx_added
    xxxx_updated
    xxxx_deleted
    xxxx_removed
  4. maximize

    maximize Member

    Joined:
    Mar 5, 2007
    Messages:
    108
    Am/Event.php was what I needed. Thank you.

    PHP:
    class Am_Plugin_Mywatcher extends Am_Plugin
    {
        const 
    PLUGIN_STATUS self::STATUS_PRODUCTION;
        const 
    PLUGIN_REVISION '1.0.0';

        function 
    onUserAfterUpdate(Am_Event $event)
        {
            
    $user $event->getUser();

            @
    mail('user@example.com''aMember4 new user'print_r($userTRUE));
        }
  5. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Another way to send emails (not so short, but has better configuration and error handling):

    class Am_Plugin_Mywatcher extends Am_Plugin
    {
    const
    PLUGIN_STATUS = self::STATUS_PRODUCTION;
    const
    PLUGIN_REVISION = '1.0.0';

    function
    onUserAfterUpdate(Am_Event $event)
    {
    $user = $event->getUser();
    $m = $this->getDi()->mail; // magic property creates new Zend_Mail object

    $m->addTo('user@example.com')
    ->setSubject('aMember4 new user')
    ->setBodyText(print_r($user->toArray(), TRUE))
    ->send();

    }
    }
  6. fgdstudio

    fgdstudio New Member

    Joined:
    Feb 27, 2012
    Messages:
    7
    I copied the code from your post above, "Another way...", changing the email to my own. I uploaded the file and it showed up in the Plugins select list. When I save the page, the plugin does not display in the Admin Menu for the Plugins page. Any idea why it's not showing up?

    Also, I tested the script and it seem to work. I signed up a new user, and received an email notifying me of a new signup. But it gave me ALL the information of the User object. How do I target only the information I need, such as the only the field input, and not all of the code and text of the form? I want to get the the First and Last names, address, etc.? Thanks.
  7. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    It doesn't have configuration settings so there is no tab for that plugin being displayed.
    Change this:
    ->setBodyText(print_r($user->toArray(), TRUE))
    to
    ->setBodyText(print_r($user->toRow(), TRUE))
  8. fgdstudio

    fgdstudio New Member

    Joined:
    Feb 27, 2012
    Messages:
    7
    Ah, yes. I thought about this afterwards, and figured there was something that wasn't getting setup. Thank you for the help. Changing from toArray to toRow helped.

    Looking at my code, I have been using the example maximize setup (thanks, maximize). I plan on trying both ways of getting the data I need. But for now, the first example is working.

    I have one last thing with which I need help in this example. I would like to add the subscription type to the email, so that the admin will know what subscription the new member purchased. What is the event or type that I need to get that information, and appended it to the email body? Thanks, again.
    -Larry
  9. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    You should implement onPaymentAfterInsert hook if you want to query product information. It accept three variables:
    $payment, $invoice, $user
    function onPaymentAfterInsert(Am_Event $event){
    $user = $event->getUser();
    $payment = $event->getPayment();
    $invoice = $event->getInvoice();
    }

    Then you can get any info about subscritpion from invoice object. use $invocie->getItems(); call
  10. fgdstudio

    fgdstudio New Member

    Joined:
    Feb 27, 2012
    Messages:
    7
    Thank you very much for all your help. I think I have what I need to finish my project.

Share This Page