Reading list Switch to dark mode

    Register customer from module front controller PrestaShop 1.7

    Updated 21 January 2022

    In some situations, we need to implement the customer registration from a different/custom module page. Today we are going to understand the code implementation for the same.

    The customer registration form
    The customer is logged in still getting able to register/create a customer through the module’s front controller

    In this code implementation, we are not going to write a code for the customer registration form rather we will just use the same tpl file which is being used by Prestashop.

    We will understand the process in three steps display blank form, save the input/submit form, and edit the form.

    Display a blank form

    To display the native Prestashop customer registration form we just need to add the below code in the initContent() function of our front controller.

    $registerForm = $this->makeCustomerForm()->setGuestAllowed(false)->fillWith(Tools::getAllValues());
    $this->setTemplate('customer/registration');
    $this->context->smarty->assign($registerForm->getTemplateVariables());
    $this->context->smarty->assign(
        array(
            'register_form' => $registerForm->getProxy(),
            'action' => $this->context->link->getPageLink(
                'authentication',
                true,
                null,
                array('create_account'=>1)
            ),
            'hook_create_account_top' => Hook::exec('displayCustomerAccountFormTop'),
        )
    );
    $registerForm->fillWith(Tools::getAllValues());
    $this->render('customer/_partials/customer-form.tpl');

    The above block of code is creating an object of the registration form. And then assigning the template variables with hook data and then rendering the default tpl form.

    Save/Create the customer (submit the form)

    To submit the form we just need to pass the form values in fillwith() function as a parameter. And then need to call the submit() function of the registration class.

    Searching for an experienced
    Prestashop Company ?
    Find out More
    $registerForm->fillWith(Tools::getAllValues());
    $hookResult = array_reduce(
        Hook::exec('actionSubmitAccountBefore', array(), null, true),
        function ($carry, $item) {
            return $carry && $item;
        },
        true
    );
    if ($hookResult) {
        $registerForm->submit();
    }

    Edit/Load the form with existing details

    $objCustomer = new Customer(Tools::getValue('id_customer'));
    $registerForm->fillFromCustomer(
        $objCustomer
    );

    Here we are getting the customer id from the URL and creating the customer object. This customer object is getting used to fill the form details.

    So this is the process of how you can get register the customer from your controller page without creating a separate customer registration form.

    Hope after this blog, You will be able to use it easily.

    Also, do let us know about your views in the comments.

    Support

    For any kind of technical assistance or query, please raise a ticket at http://webkul.uvdesk.com or send us a mail at [email protected]

    Also, please explore our PrestaShop development services & vast range of featureful PrestaShop Addons.

    Thanks for reading. Happy coding. 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home