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 works and the gift certificate is called as an order?