cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - Custom Registration Form For A Specific Customer Group

SOLVED

Magento 2 - Custom Registration Form For A Specific Customer Group

I am trying to build a custom registration form that can be shown in CMS page for a specific group of customers. Customers whoever register through this form will fall under that group. Please help me to give an idea of how to start.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hi @kalyanchakri 

 

Here are the steps can help you to create a new registration form.

 

1) Create a new phtml file with the registration form code. (Copy code from register.phtml and ad in new file)


Example: 

/app/design/frontend/themePackageName/themeName/Magento_Customer/templates/form/newregister.phtml


2) You can use this file in content page like:

 

{{block class="Magento\Customer\Block\Form\Register" template="Magento_Customer::form/newregister.phtml"}}

3) Then you need to create a module for update the customer group. You need to create a Observer for customer_register_success event. You can set a hidden filed in form for trace form submitted. Here is the reference for create an Observer module.

 

I hope it will help you.

 

Thanks

 

If answer is helpful, Please give 'Kudos' and accept 'Answer as Solution'.

View solution in original post

14 REPLIES 14

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hi @kalyanchakri 

 

Here are the steps can help you to create a new registration form.

 

1) Create a new phtml file with the registration form code. (Copy code from register.phtml and ad in new file)


Example: 

/app/design/frontend/themePackageName/themeName/Magento_Customer/templates/form/newregister.phtml


2) You can use this file in content page like:

 

{{block class="Magento\Customer\Block\Form\Register" template="Magento_Customer::form/newregister.phtml"}}

3) Then you need to create a module for update the customer group. You need to create a Observer for customer_register_success event. You can set a hidden filed in form for trace form submitted. Here is the reference for create an Observer module.

 

I hope it will help you.

 

Thanks

 

If answer is helpful, Please give 'Kudos' and accept 'Answer as Solution'.

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Thanks @PankajS_Magento  it worked.

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hey!

Could you guys post a full example of how the custom register.html file would link into the controller?

 

Greatly appreciated if you could.

 

Thanks

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hi  @nickwellings , 

No need to have a custom controller for this. Create a CMS page and add the following code in the content area. When the CMS page is loaded, it will render the new template file which is using the core Register block.

{{block class="Magento\Customer\Block\Form\Register" template="Magento_Customer::form/newregister.phtml"}}

 

--------
Give Kudos if it helped or Accept it as a solution if it worked for you

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Thanks for replying! Smiley Very Happy

 

I may have gotten myself confused so it may help if I explain what Im hoping to achieve.

 

Im wanting to keep the default registration form as is as this is what most customers will use.

 

However I am wanting to create a new registration form that looks the same as the default one. The only difference will be that the form is accessible on a CMS page and upon submitting the form it adds the user to a different user group. For example group ID 5.

 

I've created an observer based on the link in the solution and CMS page that calls the new registration form but now Im a bit stuck on what to do next.

 

Thanks,

Nick

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hi @nickwellings ,

 

I am assuming that you are able to load the CMS page and Observer is getting called after registration.

 

In the new registration form add a hidden field with group id. This you can make dynamic by keeping a configuration field in the admin. Try with static value first.

 

<input type="hidden" name="group_id" value="3">

 

Here is the complete observer code that I used. Please make sure you are adding additional validations or logic you want. Hope this will help you.

<?php

namespace Module\Namespace\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Message\ManagerInterface;

Class SaveCustomerGroupId implements ObserverInterface {
    public $_customerRepositoryInterface;
    public $_messageManager;
    public function __construct(
            CustomerRepositoryInterface $customerRepositoryInterface,
            ManagerInterface $messageManager
    ) {
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
        $this->_messageManager = $messageManager;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer) {
       $accountController = $observer->getAccountController();
       $request = $accountController->getRequest();
       $group_id = $request->getParam('group_id');
       
       try {
           $customerId = $observer->getCustomer()->getId();
           $customer = $this->_customerRepositoryInterface->getById($customerId);
           $customer->setGroupId($group_id);
           $this->_customerRepositoryInterface->save($customer);
           
       } catch (Exception $e){
           $this->_messageManager->addErrorMessage(__('Something went wrong! Please try again.'));
       }
    }
}

--------
Give Kudos if it helped or Accept it as a solution if it worked for you

 

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Thanks! I'll give this a go. I was able to get my registration form to load in the CMS page. I'm unsure how to check if the observer is getting called after registration.

 

I think my stumbling block is that I don't know how to use that observer in my registration form.

 

The only change in my form compared at the default is the addition of:

 

<input type="hidden" name="group_id" value="82">

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Hi @nickwellings ,

 

Please add "events.xml" under "app/code/Namespace/Module/etc/frontend" and keep the observer file under "app/code/Namespace/Module/Observer". In this example, my observer file name is "SaveCustomerGroupId.php".

This XML will help to call the observer whenever the event "customer_register_success" triggers. This is a default Magento event which gets called after registration success.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_register_success">
<observer name="save_customer_group" instance="Module\Namespace\Observer\SaveCustomerGroupId" />
</event>
</config>

--------
Give Kudos if it helped or Accept it as a solution if it worked for you

Re: Magento 2 - Custom Registration Form For A Specific Customer Group

Ah I see! Smiley Very Happy

 

Would this observer be called for the default registration form and any other registration forms I create too? If so I assume because the default form doesn't have the hidden field 'group_id' it would use the default group?