Gift_certificates for backend order_management

Hello everyone, on the order_management admin panel page, I created a form that displays a gift certificate as an order.
Code:

<form action="" method="POST">
    <label for="gift_cert_data">Подарочные сертификаты</label>
    <select name="gift_cert_data" id="gift_cert_data"  class="cm-object-selector toll-select"
            data-ca-enable-images="false"
            data-ca-enable-search="true"
            data-ca-close-on-select="false"
            data-ca-allow-clear="true">
        {foreach from=$gift_certificates item="code"}
            {if $code}
                {$label=$code|fn_get_gift_certificate_templates:"cart"}
                <option value="{$gift_cert_data.gift_cert_code}" selected="selected" {if $items.$code.required}class=" selectbox-highlighted cm-required"{/if}>{$code}</option>
            {/if}
        {/foreach}
        {foreach from=$items item="code" key="code"}
            {if $item.required && !$code|in_array:$gift_cert_data}
                {$label=$code|fn_save_cart_content:"cart"}
                <option value="{$gift_cert_data.gift_cert_code}" selected="selected"  class="selectbox-highlighted cm-required">{$code}</option>
            {/if}
        {/foreach}
    </select>
    <a class="btn btn-primary cm-ajax" data-ca-target-id="{$gift_cert_id} href="{"order_management.add"|fn_url}">Добавить</a>
</form>

I also copied a snippet of code from frondtend and slightly changed the backend controller of the gift certificate
func:

<?php
/* LTIM */

use Tygh\Registry;

if (!defined('BOOTSTRAP')) { die('Access denied'); }

$cart = & Tygh::$app['session']['cart'];
$suffix = !empty($cart['order_id']) ? '.update' : '.add';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    /** @var $mode */
    if ($mode == 'add') {
        if (!empty($_REQUEST['gift_cert_data']) && is_array($_REQUEST['gift_cert_data'])) {

            $gift_cert_data = $_REQUEST['gift_cert_data'];

            // Cart is empty, create it
            if (empty(Tygh::$app['session']['cart'])) {
                fn_clear_cart(Tygh::$app['session']['cart']);
            }
            unset(Tygh::$app['session']['cart']['product_groups']);

            if (!empty($_REQUEST['gift_cert_data']['email']) && !fn_validate_email($_REQUEST['gift_cert_data']['email'], true)) {
                if (defined('AJAX_REQUEST')) {
                    exit();
                } else {
                    return array(CONTROLLER_STATUS_OK, 'order_management.add');
                }
            }

            // Gift certificates is empty, create it
            if (empty(Tygh::$app['session']['cart']['gift_certificates'])) {
                Tygh::$app['session']['cart']['gift_certificates'] = array();
            }

            $previous_cart_total = isset(Tygh::$app['session']['cart']['total']) ? floatval(Tygh::$app['session']['cart']['total']) : 0;
            list($gift_cert_id, $gift_cert) = fn_add_gift_certificate_to_cart($gift_cert_data, $auth);

            if (!empty($gift_cert_id)) {
                Tygh::$app['session']['cart']['gift_certificates'][$gift_cert_id] = $gift_cert;
                $gift_cert['gift_cert_id'] = $gift_cert_id;

                $gift_cert['display_subtotal'] = $gift_cert['amount'];
                fn_calculate_cart_content(Tygh::$app['session']['cart'], $auth, 'S', true, 'F', true);
                $gift_cert['display_subtotal'] = Tygh::$app['session']['cart']['gift_certificates'][$gift_cert_id]['display_subtotal'];

                Tygh::$app['view']->assign('gift_cert', $gift_cert);
                $msg = Tygh::$app['view']->fetch('views/checkout/components/product_notification.tpl');
                fn_set_notification('I', __('gift_certificate_added_to_cart'), $msg, 'I');
            }

            fn_save_cart_content(Tygh::$app['session']['cart'], $auth['user_id']);

            if (defined('AJAX_REQUEST')) {
                fn_calculate_cart_content(Tygh::$app['session']['cart'], $auth, false, false, 'F', false);
            }
        }

        return array(CONTROLLER_STATUS_OK, 'order_management.add' . $suffix);
    }
    else {

        fn_add_breadcrumb(__('gift_certificates'));

        Tygh::$app['view']->assign('templates', fn_get_gift_certificate_templates());
        Tygh::$app['view']->assign('states', fn_get_all_states());
        Tygh::$app['view']->assign('countries', fn_get_simple_countries(true, CART_LANGUAGE));

    }

    if ($mode == 'verify') {

        if (!defined('AJAX_REQUEST') && empty($_REQUEST['verify_code'])) {
            return [CONTROLLER_STATUS_REDIRECT, 'gift_certificates.add'];
        }

        fn_add_breadcrumb(__('gift_certificate_verification'));

        $verify_id = db_get_field("SELECT gift_cert_id FROM ?:gift_certificates WHERE gift_cert_code = ?s ?p", $_REQUEST['verify_code'], fn_get_gift_certificate_company_condition('?:gift_certificates.company_id'));
        if (!empty($verify_id)) {
            Registry::set('navigation.tabs', array (
                'detailed' => array (
                    'title' => __('detailed_info'),
                    'js' => true
                ),
                'log' => array (
                    'title' => __('history'),
                    'js' => true
                )
            ));

            $params = $_REQUEST;
            $params['gift_cert_id'] = $verify_id;
            list($log, $search) = fn_get_gift_certificate_log($params, Registry::get('settings.Appearance.elements_per_page'));

            Tygh::$app['view']->assign('log', $log);
            Tygh::$app['view']->assign('search', $search);

            $verify_data = fn_get_gift_certificate_info($verify_id, 'B');
            if (false != ($last_item = reset($log))) {
                $verify_data['amount'] = $last_item['debit'];
                $verify_data['products'] = $last_item['debit_products'];
            }

            Tygh::$app['view']->assign('verify_data', $verify_data);
        } else {
            fn_set_notification('W', __('warning'), __('error_gift_cert_code'));

            if (defined('AJAX_REQUEST')) {
                exit();
            }
        }
    }
    //
    // Delete attached certificate
    //
    if ($mode == 'delete_use_certificate') {
        fn_delete_gift_certificate_in_use($_REQUEST['gift_cert_code'], $cart);

        return array(CONTROLLER_STATUS_REDIRECT, 'order_management' . $suffix);
    }

    if ($mode == 'delete_certificate') {
        if (!empty($_REQUEST['gift_cert_cart_id'])) {
            fn_delete_cart_gift_certificate($cart, $_REQUEST['gift_cert_cart_id']);

            return array(CONTROLLER_STATUS_REDIRECT, 'order_management' . $suffix);
        }
    }
    return;
}

//
// Display totals
//
/** @var $mode */
if ($mode == 'update' || $mode == 'add') {
    $gift_certificate_condition = (!empty($cart['use_gift_certificates'])) ? db_quote(" AND gift_cert_code NOT IN (?a)", array_keys($cart['use_gift_certificates'])) : '';
    Tygh::$app['view']->assign('gift_certificates',
        db_get_fields(
            "SELECT gift_cert_code FROM ?:gift_certificates WHERE status = 'A' ?p",
            $gift_certificate_condition . fn_get_gift_certificate_company_condition('?:gift_certificates.company_id')
        )
    );

    if (!empty($_REQUEST['gift_cert_id']) && !isset(Tygh::$app['session']['cart']['gift_certificates'][$_REQUEST['gift_cert_id']])) {
        return array(CONTROLLER_STATUS_REDIRECT, 'gift_certificates.add');
    }

    fn_add_breadcrumb(__('gift_certificates'));

    if (!empty($_REQUEST['gift_cert_id'])) {
        $gift_cert_data = fn_get_gift_certificate_info($_REQUEST['gift_cert_id'], 'C');

        if (!empty($gift_cert_data['extra']['exclude_from_calculate'])) {
            return array(CONTROLLER_STATUS_NO_PAGE);
        }

        Tygh::$app['view']->assign('gift_cert_data', $gift_cert_data);
        Tygh::$app['view']->assign('gift_cert_id', $_REQUEST['gift_cert_id']);
    }

    Tygh::$app['view']->assign('templates', fn_get_gift_certificate_templates());
    Tygh::$app['view']->assign('states', fn_get_all_states());
    Tygh::$app['view']->assign('countries', fn_get_simple_countries(true, CART_LANGUAGE));
    Tygh::$app['view']->assign('type', 'C');
}

How can I submit this form so that the fn_get_gift_certificate_templates function will work and call the gift certificate as an order

IThe form submit button should refer to the controller which contains the code you need - an existing one (if your form structure matches the original), or you can create a custom one and process your form data there.

Best regards,
Robert

Thanks for the answer! Yes, I understand your idea. You want to say that when the button is activated, the js script is triggered, which passes through the controller?

Yes, from what I know, you cannot do any operations in CS-Cart through pure JS. If the form is sent somewhere, it has to refer to a controller.
But I don’t know exactly what effect do you want to achieve, so I cannot say with 100% confidence which solution will work for you.

Best regards,
Robert

Hello @soft-solid :wave: There is select with certificates. When you select a certificate on the order_management page, it, like the product, becomes an order. We want to make this functionality.