如何在WHMCS钩子中获取客户服务自定义字段值(AfterModuleCreate场景)
Hey there, let's walk through exactly how to pull those custom field values when the AfterModuleCreate hook triggers in WHMCS. I've implemented this dozens of times for various use cases, so here's the reliable, step-by-step approach:
First, a quick reminder: the AfterModuleCreate hook passes a $params array that includes the serviceid — this is your golden ticket to accessing the service's custom fields.
Option 1: Use WHMCS's Built-in API (Recommended)
WHMCS has a native API endpoint GetClientsProducts that fetches all service details, including custom fields. This is the safest method because it handles data sanitization and formatting for you automatically.
Here's a complete hook example:
add_hook('AfterModuleCreate', 1, function($params) { // Grab the service ID from the hook parameters $serviceId = $params['serviceid']; // Call the GetClientsProducts API to get service details $serviceResponse = localAPI('GetClientsProducts', [ 'serviceid' => $serviceId, 'stats' => false // Skip unnecessary stats to optimize performance ]); // Check if the API call was successful if ($serviceResponse['result'] === 'success' && !empty($serviceResponse['products']['product'])) { $serviceData = $serviceResponse['products']['product']; $customFields = $serviceData['customfields']['customfield']; // Loop through custom fields to find the one you need foreach ($customFields as $field) { // Replace with your actual custom field name OR field ID if ($field['name'] === 'Preferred Support Timezone') { $customFieldValue = $field['value']; // Execute your desired action here // Example: Log the value to WHMCS activity log logActivity("Fetched custom field value '$customFieldValue' for service ID $serviceId"); // Break the loop once we find our target field break; } } } });
Option 2: Direct Database Query (For Advanced Control)
If you prefer to interact directly with the database (just make sure to use WHMCS's built-in functions to avoid SQL injection), you can query the tblhostingcustomfields table where relid matches the service ID.
Example code snippet:
add_hook('AfterModuleCreate', 1, function($params) { $serviceId = $params['serviceid']; // Query the database for the specific custom field $result = select_query( 'tblhostingcustomfields', 'value', [ 'relid' => $serviceId, 'fieldname' => 'Preferred Support Timezone' // Replace with your field name ] ); $fieldData = mysql_fetch_array($result); if ($fieldData) { $customFieldValue = $fieldData['value']; // Perform your operation here logActivity("Direct DB fetch: Custom field value is '$customFieldValue' for service $serviceId"); } });
Important Notes to Remember:
- Use Field ID Instead of Name (Optional): For better reliability (in case you rename the custom field later), replace
fieldnamewithidand use the field's numeric ID instead of its display name. - Handle Multi-Select Fields: If your custom field is a multi-select, the
valuewill be a comma-separated string. You can split it usingexplode(',', $customFieldValue)if needed. - Hook Placement: Save your hook file in the
includes/hooks/directory of your WHMCS installation (e.g.,custom_service_custom_fields_hook.php). Make sure file permissions are set correctly (usually 644). - Test Thoroughly: Create a test service to trigger the hook, then check the Activity Log (Setup > Logs > Activity Log) to verify the custom field value is being retrieved correctly.
内容的提问来源于stack exchange,提问作者Mohammad J




