Jump to content

Assign client automatically to Client Group during registration


Tapeix

Recommended Posts

I'd like to automatically assign a client to a Client Group during registration, based on the url OR template they're visiting. I've tried to find more information in the documentation, but I'm afraid there isn't much information about this subject either. 

Do know if Is it possible to:

  • Set Client Group by adding a parameter to the url?

I'm looking forward to your thoughts! 

Link to comment
Share on other sites

That's a great idea @brian! I've tried to write a little hook by myself. Unfortunately I couldn't get it to work. Any thoughts?

<?php
add_hook('ClientAdd', 1, function($vars) {
        return array(
            'create' => true,
            'groupid' => 3,
        );
    }
);

The URL if statement would look something like this:

$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'CNAMEdomain') !== false) { }
Edited by Tapeix
Link to comment
Share on other sites

ClientAdd doesn't support a return, so you need to do the needed items within the hook;

<?php
add_hook('ClientAdd', 1, function($vars)
{
    try
    {
        $client = \WHMCS\User\Client::find($vars['userid']);
        if ($client)
        {
            $client->groupid = 3;
            $client->save();
        }
    }
    catch (Exception $e)
    {
        //TODO error handler
    }


});

 

Link to comment
Share on other sites

That's amazing Steven! I've added the URL if-then-else and tried it in our live environment. I can confirm it works. 

<?php
add_hook('ClientAdd', 1, function($vars) {
    $url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $client = \WHMCS\User\Client::find($vars['userid']);

    try {
            if ($client) {
                if (strpos($url, 'CNAMEdomain.com') !== false) {
                    $client->groupid = 3;
                    $client->save();
                } else /** Default group **/ {
                    $client->groupid = 1;
                    $client->save();
                }
            }
        }

    catch (Exception $e) {
            //TODO error handler
    }

});

p.s. where did you learn that ClientAdd doesn't support a return? I couldn't find it in the documentation.

Link to comment
Share on other sites

14 hours ago, Tapeix said:

where did you learn that ClientAdd doesn't support a return? I couldn't find it in the documentation.

the docs states...

Quote

Response: No response supported

usually, the hooks do one of three things - return HTML (e.g the output hooks), return arrays (e.g the areapage hooks), or return nothing - which means you use them internally like @steven99 has above - nice hook btw... and saves me having to write it! thanks.png

one thought on your tweak @Tapeix - and this depends on what you mean by "default group", because when a client registers with WHMCS, their default client group value is 0 (e.g they're not in a client group)... so if that's what you want to do for those who aren't coming from your URL, then you would set it to 0 rather than 1 (actually, you wouldn't even bother to set it to 0 as it would do that by itself unless you tell it otherwise)... however, if you are wanting new registrations to be in either groups 3 or 1 (and they both exist) then the hook is fine.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated