I'm glad you got it sorted. Thanks for reporting back.
<?php // Copyright Pixel Crescent 2014 All Rights Reserved // This initialises all MODx Commands and Permissions! :D error_reporting(E_ALL); require_once '../config.core.php'; require_once MODX_CORE_PATH.'model/modx/modx.class.php'; $modx = new modX(); $modx->initialize('web'); $modx->getService('error','error.modError', '', ''); function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) { global $modx; //mail('[email protected]', 'Just received a purchase!', 'The user: '.$username.' at '.$payer_email.' has just purchased an item for $'.$payment_amount.' ID: '.$item_id); $myUser = $modx->getObject('modUser', array('username'=>$username)); if ($myUser) { $myUser->save(); $myUser->joinGroup($item_id); echo 'It is set!'; $cm = $modx->getCacheManager(); $cm->refresh(); } else { mail('[email protected]', 'Error Assigning group', 'Something went wrong when assigning the user '.$username.' who purchased the item '.$item_id.' for'.$payment_amount); die('No User'); } } ?>
<?php include('userPurchase.php'); userPurchasedItem('4', '5', '[email protected]', 'muffinjello'); ?>
They might, though I think they would have it the next time they log in. You can clear the cache in the code with:
$cm = $modx->getCacheManager(); $cm->refresh();
That clears the entire site cache, though. It's possible to clear just the relevant parts of the cache, but it's fairly complex and difficult to set up.
$cm = $modx->getCacheManager(); $cm->refresh();
You have a function that adds a user to a User Group, but you're never calling that function in your code (at least not in the code you've posted).
This line at the end would add me to group 4 (assuming that I was in your database):
userPurchasedItem(12, 24.95, [email protected], 'BobRay');
FYI, you can send the ID of a role as the second argument to joinGroup() if you want to assign the user a role in the group.
<?php include('userPurchase.php'); userPurchasedItem('001', '25', '[email protected]', 'muffinjello'); ?>
userPurchasedItem(12, 24.95, [email protected], 'BobRay');
It's in a function. You have to declare the $modx variable global.
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) { global $modx;
BobRay explains here http://forums.modx.com/thread/15948/when-use-global-modx#dis-post-86192
$modx->initialize('web');
<?php // Copyright Pixel Crescent 2014 All Rights Reserved // This initialises all MODx Commands and Permissions! :D require_once '../config.core.php'; require_once MODX_CORE_PATH.'model/modx/modx.class.php'; $modx = new modX(); $modx->initialize('web'); $modx->getService('error','error.modError', '', ''); function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) { global $modx; $myUser = $modx->getObject('modUser', array('username'=>$username)); if ($myUser instanceof modUser) { $myUser->joinGroup('4'); echo 'user Success!'; } else { die('No User'); } } ?>
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) { global $modx;