1. 2 december 2019
  2. Tips

The following action hook lets you automatically assign a Customer to a specific Client Group based on the Product/Service he has just purchased. The script triggers as soon as you the order is accepted on WHMCS both manually and automatically. Read comments to adapt the script to your needs.
<?

/**
 * Assign Client to Group based on purchased Product/Service
 *
 * @package     WHMCS
 * @copyright   Katamaze
 * @link        https://katamaze.com
 * @author      Davide Mantenuto <[email protected]>
 */

use WHMCS\Database\Capsule;

add_hook('AcceptOrder', 1, function($vars)
{
    $productID  = 12; // Replace with the ID of your Product/Service
    $groupID    = 1; // Replace with the ID of your Client Group
    $userID     = Capsule::table('tblorders')->leftJoin('tblhosting', 'tblorders.id', '=', 'tblhosting.orderid')->where([['tblorders.id', '=', $vars['orderid']], ['tblhosting.packageid', '=', $productID]])->first(['tblorders.userid']);

    if ($userID)
    {
        Capsule::table('tblclients')->where('id', $userID->userid)->update(['groupid' => $groupID]);
    }
});

First seen on whmcs.community - Posted by me

Written by

Davide - Founder, Developer