我一直在尝试设置 WHMCS 实例,以根据产品自动将客户端分配到客户端组。
我从 Katamaze 找到了一个代码钩子,它以前似乎有效,但现在在带有 WHMCS 8.8 的 PHP 8.1 上抛出如下错误 - 似乎修复了一个漏洞,导致新 PHP 中的一些代码发生了变化,打破了这个钩子。
<?php
/**
* Assign Client to Group based on purchased Product/Service v1
*
* @package WHMCS
* @copyright Katamaze
* @link https://katamaze.com
* @author Davide Mantenuto <[email protected]>
*/
use WHMCS\Database\Capsule;
add_hook('AcceptOrder', 1, function($vars)
{
// Define group/product pairs. Instructions provided below
// https://github.com/Katamaze/WHMCS-Free-Action-Hooks/blob/master/README.md#client-to-group-based-on-purchased-productservice
$groups['products']['5'] = array('3', '18', '16', '5','6','7','8','9','10','11','12','13','14','15','19');
$groups['products']['6'] = array('75','76');
$groups['productaddons']['0'] = array('0');
$groups['configurableoption']['0'] = array('0' => array('0'), '' => true);
if (!$groups): return; endif;
$userID = Capsule::table('tblorders')->where('id', $vars['orderid'])->pluck('userid')[0];
$orderedProducts = Capsule::table('tblhosting')->where('orderid', $vars['orderid'])->pluck('packageid', 'id');
$orderedProductAddons = Capsule::table('tblhostingaddons')->where('orderid', $vars['orderid'])->pluck('addonid');
if ($groups['configurableoption'])
{
foreach (Capsule::select(Capsule::raw('SELECT t1.relid, t1.configid, t1.optionid, t1.qty, t2.optiontype FROM tblhostingconfigoptions as t1 LEFT JOIN tblproductconfigoptions AS t2 ON t1.configid = t2.id LEFT JOIN tblproductconfigoptionssub AS t3 ON t1.optionid = t3.id WHERE t1.relid IN (\'' . implode('\',\'', array_keys($orderedProducts)) . '\')')) as $v)
{
$relid = $v->relid;
$configid = $v->configid;
if (in_array($v->optiontype, array('3', '4')))
{
$value = ($v->qty ? true : false);
}
else
{
$value = $v->optionid;
}
unset($v);
if ($value)
{
$orderedConfigurableOptions[$relid][$configid] = $value;
}
}
}
foreach ($groups['products'] as $group => $target)
{
if (array_intersect($orderedProducts, $target))
{
Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
return;
}
}
foreach ($groups['productaddons'] as $group => $target)
{
if (array_intersect($orderedProductAddons, $target))
{
Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
return;
}
}
foreach ($groups['configurableoption'] as $group => $configurableOptions)
{
foreach ($configurableOptions as $configID => $options)
{
if (is_array($options))
{
foreach ($orderedConfigurableOptions as $target)
{
if (array_intersect($options, $target))
{
Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
return;
}
}
}
else
{
foreach ($orderedConfigurableOptions as $target)
{
if (in_array($configID, $target))
{
Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
return;
}
}
}
}
}
});
我怀疑如果我回到 PHP 7.x,这可能会起作用,但这是一项相当大的任务,并且需要对代码进行大量更改......所以我还没有尝试,希望有人能修复!
修复:
if (is_array($orderedProducts) && is_array($target)) {
if (array_intersect($orderedProducts, $target)) {
// ...
}
}
和
$userID = Capsule::table('tblorders')->where('id', $vars['orderid'])->pluck('userid');
if(is_array($userID)) {
$userID = $userID[0] ?? null;
}