We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10405
    • 288 Posts
    I need to set up an ecommerce site in MODX for someone (MODX because of the multiple user groups and access levels to pages possible) and they want a user's group to change when they make a certain purchase through the system.

    Basically, a registered user will have access to certain additional pages, but to access another set of restricted pages, they need to purchase a membership. When the purchase is made, they automatically become a member of the group with access to these additional pages- they power up to the next group basically.

    I know that the client could do all this himself by simply changing the user's group membership manually in the backend when the membership purchase notification comes through. But for some reason he thinks the few seconds to do this is unacceptable so "needs" it to be automatic.

    Does anyone know if this can be done, and if so, how?

    Thanks
      • 3749
      • 24,544 Posts
      Adding the user to a group in code is trivial:

      $user->joinGroup('GroupName);


      but where to put that code isn't.

      If the user's registration purchase notification comes via email, MODX won't even be involved, so there's no way MODX can automatically put the user in the group. Depending on the system used to let users pay, there may be a way to have the payment system (e.g., PayPal), send an Ajax request to a page that will put the user in the group. The code for that would depend on what information you're getting from the payment system and it would have to include authentication and retrieval of the user object as well.
        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
        • 10405
        • 288 Posts
        Thanks for the reply. The client has a Paypal Premium account, so if that can be configured so that Paypal sends the user to a confirmation page that we specify on the site, can we then simply have a snippet on that one page that automatically adds the user to the group?

        Problem is, how will Paypal know the username to add to the group and therefore be able to add it...

        We would also need that page to be restricted in some way, or else it could be misused (if it has a snippet that adds the logged-in user to the group)

        The client seems to think it can be done in Drupal, but I'm having to create the site with a highly bespoke design therefore using MODx.

        It's all because he refuses to spend a few seconds adding the user to the group when the purchase notification comes in.
          • 3749
          • 24,544 Posts
          Clients want what they want. That's fine with me as long as they're willing to pay for it. wink

          Problem is, how will Paypal know the username to add to the group and therefore be able to add it

          This is the key question. It's been a while since I looked at the PayPal API, but IIRC, you can send a user ID along with the payment submission and they'll send it back to you with the confirmation.

          As for protecting the page with the snippet that receives the confirmation, the PayPal API should take care of that. The page will have to be published, but there will be a way to tell if the information is really coming from PayPal.

          Once you know the User ID and have confirmed payment, it's just a matter of doing this:


          $user = $modx->getObject('modUser', $userId);
          
          if ($user) {
              $user->joinGroup('GroupName');
          } else {
             // user not found
          }
          

            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