2

My users have an accountBalance field and there's a createInvoice service in my plugin. I'd like to autogenerate an invoice whenever the accountBalance changes (including from the admin cp), while optionally passing in extra invoice info. Is there a way to access the raw post data from the event to check for extra info like invoice skus and descriptions?

In my plugin I have the following code.

craft()->on('users.onBeforeSaveUser', function(Event $event)
{
    $userId = $event->params['user']->id;
    $user = craft()->users->getUserById($userId);

    //raw post data is private so these lines are pseudo code
    if ($extraPostData != null)
    {
        $desc = $event->params['_rawPostData']['invoiceDescription'];
        $sku = $event->params['_rawPostData']['invoiceSku'];
    }

    $oldBalance = $user->accountBalance;
    $newBalance = $event->params['user']->accountBalance;
    if ($newBalance != $oldBalance)
    {
        $balanceDelta = $newBalance - $oldBalance;

        //plugin service has defaults if $desc/$sku are empty or null
        craft()->myPlugin->createInvoice($balanceDelta, $userId, $desc, $sku);
    }
}

Alternatively, is there a way to disable adjusting the accountBalance user field from the admin cp?

  • 2
    Not sure I'm following 100%, but could you just pull them from $_POST? (or craft()->request->getPost())? – Brad Bell Oct 12 '15 at 20:04
  • Derp, yah that works. Didn't think $_POST would be available from within the event. Time for more coffee. Thanks for the answer. – David Rampersad Oct 12 '15 at 20:49
2

You can just grab whatever you need either from $_POST or, more preferably, craft()->request->getPost().

  • 1
    ...and if you want to get a specific variable, you can do craft()->request->getPost('invoiceSku'). – Mats Mikkel Rummelhoff Oct 12 '15 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.