1<?php
2require __DIR__ . '/init.php';
3use WHMCS\Database\Capsule;
4
5$adminUsername = 'nitrolivetv';
6$templateName = 'Automatic Setup Successful';
7
8// --- CHANGE THIS ID ---
9$orderId = 1042; // Enter the Order ID here
10// ----------------------
11
12// 1. Find the User ID associated with this Order
13$order = Capsule::table('tblorders')->where('id', $orderId)->first();
14$target_userid = $order ? $order->userid : 0;
15
16if ($target_userid > 0) {
17 // 2. Fetch the GCLID for that user
18 $gclidValue = Capsule::table('tblcustomfieldsvalues')
19 ->where('fieldid', 34)
20 ->where('relid', $target_userid)
21 ->value('value');
22
23 // 3. Find a Service ID related to this order for the 'relid' context
24 // (Optional, but helps 'Setup' templates fill in product details)
25 $service = Capsule::table('tblhosting')->where('orderid', $orderId)->first();
26 $serviceId = $service ? $service->id : $orderId;
27
28 // 4. Pass it directly into the API
29 $result = localAPI('SendAdminEmail', [
30 'messagename' => $templateName,
31 'relid' => $serviceId,
32 'mergefields' => [
33 'gclid' => $gclidValue ?: "NO-GCLID-IN-DB"
34 ],
35 'to' => 'noreply@theservice4u.com',
36 ], $adminUsername);
37
38 echo "Order: " . $orderId . " | Found User: " . $target_userid . " | GCLID: " . ($gclidValue ?: "NONE");
39 echo '<pre>' . print_r($result, true) . '</pre>';
40} else {
41 echo "Error: Could not find an order with ID " . $orderId;
42}