D
D
Dmitry2022-03-18 17:10:26
1C-Bitrix
Dmitry, 2022-03-18 17:10:26

How to create your own condition for cart rules?

Good afternoon, I'm trying to create a condition for the cart rule, I tried it according to the documentation, according to examples, the result is the same - the condition is not displayed. Maybe someone came across, tell me where to dig, thanks.

class CatalogCondCtrlUserProps extends \CCatalogCondCtrlComplex
{
    public static function GetClassName()
    {
        return __CLASS__;
    }
    /**
     * @return string|array
     */
    public static function GetControlID()
    {
        return array('CondUser', 'CondUserDestinationStore');
    }
    public static function GetControlShow($arParams)
    {
        $arControls = static::GetControls();
        $arResult = array(
            'controlgroup' => true,
            'group' =>  false,
            'label' => 'Поля Пользователя',
            'showIn' => static::GetShowIn($arParams['SHOW_IN_GROUPS']),
            'children' => array()
        );
        foreach ($arControls as &$arOneControl)
        {
            $arResult['children'][] = array(
                'controlId' => $arOneControl['ID'],
                'group' => false,
                'label' => $arOneControl['LABEL'],
                'showIn' => static::GetShowIn($arParams['SHOW_IN_GROUPS']),
                'control' => array(
                    array(
                        'id' => 'prefix',
                        'type' => 'prefix',
                        'text' => $arOneControl['PREFIX']
                    ),
                    static::GetLogicAtom($arOneControl['LOGIC']),
                    static::GetValueAtom($arOneControl['JS_VALUE'])
                )
            );
        }
        if (isset($arOneControl))
            unset($arOneControl);

        return $arResult;
    }

    /**
     * @param bool|string $strControlID
     * @return bool|array
     */
    public static function GetControls($strControlID = false)
    {
        $arControlList = array(
            'CondUser' => array(
                'ID' => 'CondUser',
                'FIELD' => 'ID',
                'FIELD_TYPE' => 'int',
                'LABEL' => 'ID Пользователя',
                'PREFIX' => 'поле ID Пользователя',
                'LOGIC' => static::GetLogic(array(BT_COND_LOGIC_EQ, BT_COND_LOGIC_NOT_EQ)),
                'JS_VALUE' => array(
                    'type' => 'input'
                ),
                'PHP_VALUE' => ''
            ),
            'CondUserDestinationStore' => array(
                'ID' => 'CondUserDestinationStore',
                'FIELD' => 'UF_USER_FIELD',
                'FIELD_TYPE' => 'string',
                'LABEL' => 'UF_USER_FIELD Пользователя',
                'PREFIX' => 'поле UF_USER_FIELD Пользователя',
                'LOGIC' => static::GetLogic(array(BT_COND_LOGIC_EQ, BT_COND_LOGIC_NOT_EQ)),
                'JS_VALUE' => array(
        'type' => 'input'
    ),
                'PHP_VALUE' => ''
            ),
        );

        foreach ($arControlList as &$control)
        {
            if (!isset($control['PARENT']))
                $control['PARENT'] = true;

            $control['EXIST_HANDLER'] = 'Y';

            $control['MODULE_ID'] = 'mymodule';

            $control['MULTIPLE'] = 'N';
            $control['GROUP'] = 'N';
        }
        unset($control);

        if ($strControlID === false)
        {
            return $arControlList;
        }
        elseif (isset($arControlList[$strControlID]))
        {
            return $arControlList[$strControlID];
        }
        else
        {
            return false;
        }
    }

    public static function checkUserField($strUserField, $strCond, $strValue)
    {
        global $USER;
        $arUser = $USER->GetByID($USER->GetID())->Fetch();

        $field = $arUser[$strUserField];

        return str_replace(array('#FIELD#', '#VALUE#'), array($field, $strValue), $strCond);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question