We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43432
    • 19 Posts
    Hello,

    I currently have some custom php code which is designed to add a specified user to a database, and it was working, but now it has stopped for whatever reason. (Did I accidentally delete something?)

    Anyways, here's the code:
    <?php
    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!'; // This is being printed when I run the test code
    		$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');
        }
    }
    ?>


    Which is being called by:

    <?php
    include('userPurchase.php');
    userPurchasedItem('4', '25', '[email protected]', 'muffinjello');
    ?>


    Since my echo
    echo 'It is set!';
    is working, I'm thinking it's more of something MODx specific that's tripping up my code. Clearing the cache is required in order for the user to join the new group (without relogging). Any help is reaaaallly appreciated!
      • 3749
      • 24,544 Posts
      See if this makes a difference:

      $myUser->joinGroup((integer) $item_id);


      BTW, no need to save the user object unless you've changed it.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 43432
        • 19 Posts
        Quote from: BobRay at Jun 21, 2014, 01:56 AM
        See if this makes a difference:

        $myUser->joinGroup((integer) $item_id);


        BTW, no need to save the user object unless you've changed it.


        Unfortunately it is still not saving. sad
          • 3749
          • 24,544 Posts
          Is $item_id the name of the user group or its ID?

          If it's the name, you may have to make it non-numeric -- e.g., 'group4' -- so MODX doesn't think it's an ID.

          I've also had issues occasionally with using 'global $modx' and ended up adding a reference variable to the function arguments (&$modx) and sending $modx as an argument.

          If nothing else works, you can create the modUserGroupMember directly (after making sure it doesn't already exist). That's a little faster anyway. I can give you an example, but need to know what $item_id is first.





            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 43432
            • 19 Posts
            Quote from: BobRay at Jun 21, 2014, 05:56 PM
            Is $item_id the name of the user group or its ID?

            If it's the name, you may have to make it non-numeric -- e.g., 'group4' -- so MODX doesn't think it's an ID.

            I've also had issues occasionally with using 'global $modx' and ended up adding a reference variable to the function arguments (&$modx) and sending $modx as an argument.

            If nothing else works, you can create the modUserGroupMember directly (after making sure it doesn't already exist). That's a little faster anyway. I can give you an example, but need to know what $item_id is first.



            Thank you for the reply, and it's the actual ID of the group itself. Although, don't worry too much about it, I'm really thankful for your help - although I've decided to just run a boxbilling installation with my MODx, since I am trying to sell things, I think this will make my life a bit easier.
              • 3749
              • 24,544 Posts
              Sometimes a canned solution is the way to go. Good luck. smiley

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting