Создать форму и обработчик [OpenCart]

01 Октября 2019
php

Задача: создать форму обратного звонка и обработать её стандартными методами опенкарта.

  1. Для начала создаем форму.

    Обычныю форму в любом месте, хоть footer.tpl(.twig) хоть модуль какой.

    Главное указать путь до обработчика, например: /index.php?route=mail/callback.

    <form action="/index.php?route=mail/callback" method="post">
        <input type="text" name="name">
        <input type="text" name="phone">
        <button type="submit">Отправить</button>
    </form>
    
  2. Теперь необходимо создать сам обработчик, храниться он может где угодно, но в нашем примере будет здесь: \catalog\controller\mail\callback.php

    class ControllerMailCallback extends Controller { 
        private $ error = array();
        public function index() { 
            $ mail = new Mail($ this->config->get('config_mail_engine'));
            $ mail->parameter = $ this->config->get('config_mail_parameter');
            $ mail->smtp_hostname = $ this->config->get('config_mail_smtp_hostname');
            $ mail->smtp_username = $ this->config->get('config_mail_smtp_username');
            $ mail->smtp_password = html_entity_decode($ this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
            $ mail->smtp_port = $ this->config->get('config_mail_smtp_port');
            $ mail->smtp_timeout = $ this->config->get('config_mail_smtp_timeout');
    
            $ mail->setTo($ this->config->get('config_email'));
            $ mail->setFrom($ this->config->get('config_email'));
            //$ mail->setReplyTo($ this->request->post['email']);
            $ mail->setSender(html_entity_decode($ this->request->post['name'], ENT_QUOTES, 'UTF-8'));
            $ mail->setSubject(html_entity_decode(sprintf('Заявка с сайта', $ this->request->post['name']), ENT_QUOTES, 'UTF-8'));
            $ mail->setHtml('
            	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
            	<html>
            	<head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                    <title>Заявка с сайта</title>
            	</head>
            		<body>
            			<p><strong>Имя</strong>: '.$ this->request->post['name'].'</p>
            			<p><strong>Телефон</strong>: '.$ this->request->post['phone'].'</p>
            			<p><strong>Время для звонка</strong>: '.$ this->request->post['other[Время звонка]'].'</p>
            		</body>
            	</html>
            ');
            $ mail->send();
    
            $ this->response->redirect($ this->url->link('information/contact/success'));
        } 
    } 
    

Важно!

  1. Класс ControllerMailCallback так называется не случайно!

  2. $ this->response->redirect($ this->url->link('information/contact/success'));
    

    Эта часть кода отправит пользователя на стандартную страницу успешной отправки формы, вы можете создать собственную или исправить существующую.