Jump to content

Add clients to custom group based on product...


EdH

Recommended Posts

Hi,

I have been trying to setup my WHMCS instance to automatically assign clients to a client group, based on the product.

I have found a code hook from Katamaze, which seems to have worked previously but it now throws an error as below on PHP 8.1 with WHMCS 8.8 - seems there was a vulnerability patched that resulted in some changed code in the new PHP, which has broken this HOOK.

TypeError: array_keys(): Argument #1 ($array) must be of type array, Illuminate\Support\Collection given in /home/cpaneluser/includes/hooks/assign-client-group-based-on-product.php:30
Stack trace:
#0 /home/cpaneluser/includes/hooks/assign-client-group-based-on-product.php(30): array_keys()
#1 [internal function]: WHMCS\Utility\SafeInclude::{closure}()
#2 /home/cpaneluser/vendor/whmcs/whmcs-foundation/lib/Hook/Manager.php(0): call_user_func()
#3 /home/cpaneluser/vendor/illuminate/support/Facades/Facade.php(261): WHMCS\Hook\Manager->run()
#4 /home/cpaneluser/includes/functions.php(0): Illuminate\Support\Facades\Facade::__callStatic()
#5 /home/cpaneluser/includes/orderfunctions.php(0): run_hook()
#6 /home/cpaneluser/admin/orders.php(0): acceptOrder()
#7 {main}

If anybody can suggest a tweak to this code, which will get it working - or perhaps another way to add new clients into a group automatically, it would be much appreciated!

<?php

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

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;
                    }
                }
            }
        }
    }
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated