How can I refresh the session data? Here's my problem, I have a custom variable (add field) that is changed after a payment is made (using a plugin to catch the product being purchased and then updating the variable accordingly). The problem is that the session variable needs to get updated so that I can show the change on the user dashboard. In V3 I just unset the $_SESSION['variable'] and it would reload it on page load. How can I force a session refresh without logging the user out and back in? I see a "session_needs_refresh" variable in the user object - can I use that somehow? Thanks
That variable affects only third-party plugins. Currently you can set it and then add hook for onAuthSessionRefresh event. In that hook just refresh user's object in session. (Let me know if this require further clarification).
so something like this? Code: require_once "amember/library/Am/Lite.php'; $user = Am_Lite::getInstance()->getUser(); $user['session_needs_refresh'] = 1; in my plugin file: Code: function onAuthSessionRefresh(Am_Event_AuthSessionRefresh $event) { how do I call the refresh here? } Also, how does the onAuthSessionRefresh get called - All I'm doing above is setting a variable. Thanks
No. I guess this is even better to update it in session when you update this variable in your plugin code: PHP: $user = Am_Di::getInstance()->auth->getUser();$user->updateQuick('YOURFIELDNAME', 'YOURFIELDVALUE');Am_Di::getInstance()->auth->setSessionVar($user->toRow());
I can just make the "setSessionVar" call after a $user->save() in my plugin to update the variables right? So the only thing that I really need to do here is to add the last line in my plugin: PHP: function onSubscriptionAdded(Am_Event $event) { $user = $event->getUser(); $product = $event->getProduct(); if($product->product_id == 6) $user->credits += 500; if($product->product_id == 7) $user->credits += 1000; $user->save(); Am_Di::getInstance()->auth->setSessionVar($user->toRow());} Right?
It could be correct if you know exactly this hook is executed always in user session. But that is not true in case if you are using system like "PayPal". In this case, payment processing happens in IPN handler, and user session cannot be updated. That it what "SESSION_NEED_REFRESH" flag used to. Most correct behaviour would be to query user credits count where you need it, instead of relying on session info - it is here only for quick reference, but not for assurance.
Another problem in code is usage of subscriptionAdded hook. This hook is only executed when user orders product that he did not have earlier. For second purchase of the same product it will not work. I will develop and send you a copy of "credits" plugin to solve your task, OK?
Plugin file attached. Configure plugin and it will add field "Credits Count" into product settings in aMember CP. The plugin creates am_credit table where it stores journal of credits and debits operations. - credit records will be added by aMember automatically. - debit records you must add via API - you can check user balance via API - you can optionally add more credits to user via API; usage examples: PHP: <?phprequire_once '/home/me/public_html/amember/bootstrap.php'; // include aMember API stack$plugin = Am_Di::getInstance()->plugins_misc->loadGet('credits');$balance = $plugin->balance(); // get balance for current customerecho "balance=$balance <br />\n";$plugin->credit(2, "Manually added 2 credits"); // add 2 credits to customer manually$plugin->debit(1, "Debited 1 credit"); // deduct 1 credit from user balanceecho "balance=$balance <br />\n"; // must be +1 to previous If you just want to display customer balance on many pages, implement kind of caching in your code, including entire aMember stack and running db queries is not cheap from performance point of view.