Jump to content

ClientAdd Hook, Save Custom field


cyberpods

Recommended Posts

Hi all, I'm trying to add a custom field to a clients account when their account is created and cant't seem to get it working.

 

This code works when I save any field on a clients account that's already created.

add_hook(
   'CustomFieldSave',
   1,
   function($vars) {
       //Check that the fieldid is one you wish to override
       if ($vars['fieldid'] == 1 && strlen($vars['value']) == '0') {          
           return array('value' => "MyCustomField");
       }
       return array();
   }
);

 

But this does not work.

add_hook(
   'ClientAdd',
   1,
   function($vars) {
       //Check that the fieldid is one you wish to override
       if ($vars['fieldid'] == 1 && strlen($vars['value']) == '0') {          
           return array('value' => "MyCustomField");
       }
       return array();
   }
);

 

What am I doing wrong?

Link to comment
Share on other sites

you need to treat each ActionHook Points differently, what is working on "CustomFieldSave" will not work in "ClientAdd", read: http://docs.whmcs.com/Hooks:ClientAdd

 

in ClientAdd, custom fields passed as array $vars['customfields'], this what you need to validate, and if you want to make changes to it, you would need to add the updated value directly in database

Link to comment
Share on other sites

Ok, I figured it out. I decided to use the internal API to make the changes. Here is the code I'm using if it can help anyone else.

 

add_hook(

'ClientAdd',

1,

function($vars) {

$userid = $vars['userid'];

$command = "updateclient";

$adminuser = "adminuser";

$values["clientid"] = $userid;

$values["customfields"] = base64_encode(serialize(array("1"=>"CustomValueStringToSave")));

$results = localAPI($command,$values,$adminuser);

return array();

}

);

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