You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在WHMCS钩子中获取客户服务自定义字段值(AfterModuleCreate场景)

How to Get Client Service Custom Field Values in WHMCS's AfterModuleCreate Hook

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.

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 fieldname with id and use the field's numeric ID instead of its display name.
  • Handle Multi-Select Fields: If your custom field is a multi-select, the value will be a comma-separated string. You can split it using explode(',', $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

火山引擎 最新活动