Back to PhocacartGroup class

Method changeUserGroupByRule

public static
changeUserGroupByRule
(mixed $userId)

Method changeUserGroupByRule - Source code

public static function changeUserGroupByRule($userId)
{
    $app = Factory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    $user_group_change_rule = $paramsC->get('user_group_change_rule', 0);
    if ($user_group_change_rule == 0) {
        // User Group Change is not enabled
        return true;
    }
    if ($userId > 0) {
        $total = PhocacartUser::getUserOrderSum($userId);
        $groups = self::getGroupRules();
        $rulesActive = 0;
        if (!empty($groups)) {
            foreach ($groups as $k => $v) {
                if ($v['minimum_sum'] > 0) {
                    $rulesActive = 1;
                }
            }
        }
        if ($rulesActive == 0) {
            // Seems like user group change rules are all empty
            return true;
        }
        $groupsNew = array();
        $groupsNewDefault = array();
        $t = 0;
        if (!empty($groups)) {
            foreach ($groups as $k => $v) {
                if ($user_group_change_rule == 2) {
                    if ($v['type'] == 1) {
                        $groupsNewDefault[$k] = $v['id'];
                        // Default is always included
                    } else {
                        if ($v['minimum_sum'] < $total || $v['minimum_sum'] == $total) {
                            $groupsNew[$k] = $v['id'];
                        }
                    }
                } else {
                    // SELECT ONLY ONE GROUP including default
                    if ($v['type'] == 1) {
                        $groupsNewDefault[$k] = $v['id'];
                        // Default is always included
                    } else {
                        if ($v['minimum_sum'] < $total || $v['minimum_sum'] == $total) {
                            if ($total > $t) {
                                $groupsNew[0] = $v['id'];
                                $t = $total;
                            }
                        }
                    }
                }
            }
        }
        $groupsAll = array_merge($groupsNew, $groupsNewDefault);
        if (!empty($groupsAll)) {
            self::storeGroupsById((int) $userId, 1, $groupsAll);
        }
        return true;
    }
    return false;
}