Jump to content

How to add my own form in home page Presta 1.7 ?


Recommended Posts

I would like to add my own form on the homepage in PrestaShop 1.7.

I add form fields in the tpl file displayed on the home page (e.g. in .tpl some module from home page). I would like to send it now with some simple php script, but it doesn't send anything to the email. How to do it?

Add php code to what class? Whether to add the php code to the new file with action = "link to the file form-send.php" Because after clicking send it does not send the form to the email. I don't want to use the built-in contact form because I have different fields with product configuration.


Below the code of the form and the php file.

 

tpl

						<form action="/form-send.php" method="post">
							<div class="col-xs-6">
								<div class="form-group">
									<label for="user" class="control-label">Name:</label>
									<input type="text" class="form-control" name="user" placeholder="Name and surname">
								</div>
								<div class="form-group">
									<label for="email" class="control-label">Email:</label>
									<input type="text" class="form-control" name="email" placeholder="e-mail address">
								</div>
								<div class="form-group">
									<label for="width" class="control-label">Width:</label>
									<input type="text" class="form-control" name="width" placeholder="cm">
								</div>
								<div class="form-group">
									<label for="length" class="control-label">Length:</label>
									<input type="text" class="form-control" name="length" placeholder="cm">
								</div>
								<div class="form-group">
									<label for="weight" class="control-label">Weight pack:</label>
									<input type="text" class="form-control" name="weight" placeholder="kg">
								</div>
								<div class="form-group">
									<button type="submit" class="btn btn-primary btn-lg">Send</button>
								</div>
							</div>
						</form>

 

php file

<?php
		$errorMessage = null;
		$successMessage = null;

		if ($_POST) {
			$user = isset($_POST['user']) ? filter_var($_POST['user'], FILTER_SANITIZE_STRING) : null;
			$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) : null;
			$width = htmlspecialchars($_POST['width']);
            $length = htmlspecialchars($_POST['length']);
            $weight = htmlspecialchars($_POST['weight']);

			if (empty($user) || empty($email) || empty($width) || empty($length)|| empty($weight)) {
				$errorMessage = 'Complete all fields!';
			}

			if (is_null($errorMessage)) {
				mail(
					'[email protected]',
					'Form from my Shop',
					"Message: $width \n\n $length \n\n $weight \n\n Name: $user \n\n E-mail: $email",
					"From: $user <$email>"
				);

				$successMessage = 'Message was sent';
			}
		}
?>

 

Edited by Adam40 (see edit history)
Link to comment
Share on other sites

I would advice to create a quick module with a front controller and then use the Prestashop Mail::Send() function.

Ill post some code here to get you going, but off-course it will not work if you just copy past it.

Creating the form link to post your data

    $this->context->smarty->assign('crezzur_postform', $this->context->link->getModuleLink($this->name, 'submitcrezzurform', [], true));

Example for .tpl file

<div id="crezzurform-data" class="card mt-3 mx-3">
    <div class="crezzur-form-start">
        <div class="card-header font-weight-bold">Your form title</div>
        <div class="contact-form card-body step">
            <form id="sendform" action="{$crezzur_postform}" name="crezzurform" method="post" enctype="multipart/form-data">
                <section class="form-fields">
                    <div class="form-group row" style="display: flex; justify-content: center;">
                        <div class="col-md-5">
                            <div class="crezz-title">Firstname <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-fname" type="text"  required="required">
                        </div>
                        <div class="col-md-5">
                            <div class="crezz-title">Lastname <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-lname" type="text"  required="required">
                        </div>
                    </div>
                    <div class="form-group row" style="display: flex; justify-content: center;">
                        <div class="col-md-5">
                            <div class="crezz-title">Email <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-mail" type="email" required="required">
                        </div>
                    </div>
                </section>
                <footer class="form-footer text-sm-right">
                    <input id="sendmyform" class="btn btn-primary" type="submit" name="sendmyform" value="Send request">
                </footer>
            </form>
        </div>
    </div>
</div>

Example of module front controller:

<?php

class MyModuleNameMyControlerNameModuleFrontController extends ModuleFrontController
{
    public function postProcess()
    {
        if ($this->ajax && !Validate::isEmail((string)Tools::getValue('crezzur-mail'))) {
            Tools::redirect('your_error_page');
        }

        $context = Context::getContext();
        $id_shop = (int) $context->shop->id;
        $id_lang = (int) $context->language->id;
        
        $fname = (string)Tools::getValue('crezzur-fname');
        $lname = (string)Tools::getValue('crezzur-lname');
        $mail = (string)Tools::getValue('crezzur-mail');

        $params = array(
            '{fname}' => $fname,
            '{lname}' => $lname,
            '{mail}' => $mail,
        );

        // SEND MAIL TO CUSTOMER
        Mail::Send(
            $id_lang,                                 // defaut language id
            'confirm_mail',                           // email template file to be use
            $this->translator->trans('We received your question!', [], 'Modules.Crezzurofferteaanvragen.Shop'),
            $params,                                  // templateVars
            $mail,                                    // to email address
            $lname . ' ' . $fname,                    // to name
            Configuration::get('PS_SHOP_EMAIL'),      // from email address
            Configuration::get('PS_SHOP_NAME'),       // from name
            false,                                    // fileAttachment
            false,                                    // modeSMTP
            _PS_MODULE_DIR_ .'crezzurmodule/mails/',  // mail template path
            false,
            $id_shop
        );

        // SEND MAIL TO STORE OWNER
        Mail::Send(
            $id_lang,                                 // defaut language id
            'admin_mail',                             // email template file to be use
            $this->translator->trans('Hello store owner, you received a message!', [], 'Modules.Crezzurofferteaanvragen.Shop'),
            $params,                                  // templateVars
            Configuration::get('PS_SHOP_EMAIL'),      // to email address
            Configuration::get('PS_SHOP_NAME'),       // to name
            $mail,                                    // from email address
            $lname . ' ' . $fname,                    // from name
            false,                                    // fileAttachment
            false,                                    // modeSMTP
            _PS_MODULE_DIR_ .'crezzurmodule/mails/',  // mail template path
            false,
            $id_shop
        );

        Tools::redirect('your_confirmation_page');
    }
}

 

Edited by Crezzur (see edit history)
Link to comment
Share on other sites

1 hour ago, Crezzur said:

I would advice to create a quick module with a front controller and then use the Prestashop Mail::Send() function.

Ill post some code here to get you going, but off-course it will not work if you just copy past it.

Creating the form link to post your data

    $this->context->smarty->assign('crezzur_postform', $this->context->link->getModuleLink($this->name, 'submitcrezzurform', [], true));

Example for .tpl file

<div id="crezzurform-data" class="card mt-3 mx-3">
    <div class="crezzur-form-start">
        <div class="card-header font-weight-bold">Your form title</div>
        <div class="contact-form card-body step">
            <form id="sendform" action="{$crezzur_postform}" name="crezzurform" method="post" enctype="multipart/form-data">
                <section class="form-fields">
                    <div class="form-group row" style="display: flex; justify-content: center;">
                        <div class="col-md-5">
                            <div class="crezz-title">Firstname <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-fname" type="text"  required="required">
                        </div>
                        <div class="col-md-5">
                            <div class="crezz-title">Lastname <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-lname" type="text"  required="required">
                        </div>
                    </div>
                    <div class="form-group row" style="display: flex; justify-content: center;">
                        <div class="col-md-5">
                            <div class="crezz-title">Email <b style="color:#dc3545;">*</b></div>
                            <input class="form-control" name="crezzur-mail" type="email" required="required">
                        </div>
                    </div>
                </section>
                <footer class="form-footer text-sm-right">
                    <input id="sendmyform" class="btn btn-primary" type="submit" name="sendmyform" value="Send request">
                </footer>
            </form>
        </div>
    </div>
</div>

Example of module front controller:

<?php

class MyModuleNameMyControlerNameModuleFrontController extends ModuleFrontController
{
    public function postProcess()
    {
        if ($this->ajax && !Validate::isEmail((string)Tools::getValue('crezzur-mail'))) {
            Tools::redirect('your_error_page');
        }

        $context = Context::getContext();
        $id_shop = (int) $context->shop->id;
        $id_lang = (int) $context->language->id;
        
        $fname = (string)Tools::getValue('crezzur-fname');
        $lname = (string)Tools::getValue('crezzur-lname');
        $mail = (string)Tools::getValue('crezzur-mail');

        $params = array(
            '{fname}' => $fname,
            '{lname}' => $lname,
            '{mail}' => $mail,
        );

        // SEND MAIL TO CUSTOMER
        Mail::Send(
            $id_lang,                                 // defaut language id
            'confirm_mail',                           // email template file to be use
            $this->translator->trans('We received your question!', [], 'Modules.Crezzurofferteaanvragen.Shop'),
            $params,                                  // templateVars
            $mail,                                    // to email address
            $lname . ' ' . $fname,                    // to name
            Configuration::get('PS_SHOP_EMAIL'),      // from email address
            Configuration::get('PS_SHOP_NAME'),       // from name
            false,                                    // fileAttachment
            false,                                    // modeSMTP
            _PS_MODULE_DIR_ .'crezzurmodule/mails/',  // mail template path
            false,
            $id_shop
        );

        // SEND MAIL TO STORE OWNER
        Mail::Send(
            $id_lang,                                 // defaut language id
            'admin_mail',                             // email template file to be use
            $this->translator->trans('Hello store owner, you received a message!', [], 'Modules.Crezzurofferteaanvragen.Shop'),
            $params,                                  // templateVars
            Configuration::get('PS_SHOP_EMAIL'),      // to email address
            Configuration::get('PS_SHOP_NAME'),       // to name
            $mail,                                    // from email address
            $lname . ' ' . $fname,                    // from name
            false,                                    // fileAttachment
            false,                                    // modeSMTP
            _PS_MODULE_DIR_ .'crezzurmodule/mails/',  // mail template path
            false,
            $id_shop
        );

        Tools::redirect('your_confirmation_page');
    }
}

 

Thank for Your reply, but

I can't write modules for PrestaShop, so I wanted to do it with a simple php script.
Can it be done somehow easier? Eg call the Mail::Send() function in my php file so that Presta sends the email using the built-in functions?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...