onAccessAfterInsert question...

Discussion in 'Customization & add-ons' started by steve_schwartz, Oct 31, 2019.

  1. steve_schwartz

    steve_schwartz New Member

    Joined:
    Jan 3, 2017
    Messages:
    10
    Hello all,

    Quick question.. I'm working on a module that will update another database that we control (it stores items from the invoice) whenever access record is created. As of now, I have it working using onPaymentWithAccessAfterInsert and I access the invoice items using $event->getInvoice();

    But... this doesn't seem to work for free subscriptions... For a free subscription, would I use onAccessAfterInsert instead? If so - how do I access the invoice items from within this function? I tried doing it the same way as onPaymentWithAccessAfterInsert, but it didn't seem to work.

    Any help is greatly appreciated!

    Thanks!
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    First of all access not always associated with invoice. Admin can add access records from admin interface without invoices. Also there is may be other cases when access record has not associated invoice record. I believe it is important to keep in mind.

    Second here is code to access invoice from onAccessAfterInsert:
    PHP:
    function onAccessAfterInsert(Am_Event $e)
    {
        
    /** @var Access $access */
        
    $access $e->getAccess();
        if (
    $access->invoice_id) {
            
    $invoice $e->getDi()->invoiceTable->load($access->invoice_id);
        }
    }
    Third this event Am_Event::ACCESS_AFTER_INSERT cover both cases free and paid subscriptions.
    So you need to remove handler onPaymentWithAccessAfterInsert, otherwise you will have two records
    in your database for same single access record in aMember.

    Best Regards.
  3. steve_schwartz

    steve_schwartz New Member

    Joined:
    Jan 3, 2017
    Messages:
    10
    perfect! thanks so much for the quick response. I will give it a try!
  4. steve_schwartz

    steve_schwartz New Member

    Joined:
    Jan 3, 2017
    Messages:
    10
    hmmm, so it looks like that part worked for me, thanks again! BUT... i must not be using the correct method to get the user data also... in my onPaymentwithAccessAfterInsert, I was able to use $event->getUser();

    Will that method not work for onAccessAfterInsert as well?
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You can get user from access record ie.:
    PHP:
    $access->getUser();
  6. steve_schwartz

    steve_schwartz New Member

    Joined:
    Jan 3, 2017
    Messages:
    10
    thanks, that worked perfect!
    caesar likes this.

Share This Page