Event for reactivated subscription?

Discussion in 'aMember Pro v.4' started by amonies, Dec 9, 2012.

  1. amonies

    amonies New Member

    Joined:
    Nov 30, 2012
    Messages:
    23
    I need to check explicitly for when someone re-activates their subscription. This is because their account will be tied into another system, which will auto terminate X number of weeks after nonpayment.

    When someone reactivates their subscription, I need to check whether their other account has been terminated or not so I know whether to re-create the account, or reactivate it there as well.

    Is there a specific event for this? Or what exact events are called when someone reactivates an old/expired/overdue subscription?

    Thanks
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    There is of course no special event, but it is easy to do.

    Hook to subscriptionAdded event - it triggers when customer subscription to a product becomes active (and earlier it was expired or not available by other reasons).
    Then in the hook you can check if user have "access" records for this product for past periods.
    Quick example

    application/default/plugins/misc/custom-plugin.php
    PHP:
    class Am_Plugin_CustomPlugin extends Am_Plugin
    {
        function 
    onSubscriptionAdded(Am_Event_SubscriptionAdded $event)
        {
              
    // fetch vars from the event
              
    $product $event->getProduct();
              
    $product_id $product->product_id;
              
    $user $event->getUser();
              
    $user_id $user->user_id;
              
    // now search access table for expired access records for the same product
              
    $found $this->getDi()->db->selectCell("SELECT COUNT(*)
                    FROM ?_access
                    WHERE user_id=?d AND product_id=?d AND expire_date < CURDATE()
              "
    $user_id$product_id);
            
    // we got number of records found to $found, here you add your custom handling
            // write to aMember Cp -> Logs
            
    if ($found 0)
            {
                    
    $this->getDi()->errorLogTable->log("User [$user_id] reactivated subscription to [$product_id]");
            } else {
                    
    $this->getDi()->errorLogTable->log("User [$user_id] has got brand new subscription to [$product_id]");
            }
         
        }
    }

Share This Page