cancel
Showing results for 
Search instead for 
Did you mean: 

How do I show Customer Groups at Registration

SOLVED

How do I show Customer Groups at Registration

Using Magento 2.2.1

Please tell me how to show Customer Groups at Registration so user can select.  I have both retail and wholesale customers.  Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How do I show Customer Groups at Registration

By Default, Magento doesn't provide this type of functionality to display drop-down for the customer group in the registration form. You need to customization of the registration page and add custom drop-down based on customer group to register page.

 

After submitting registration form you need to set Customer group based on users selected value and save programmatically customer group to that customer.

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

8 REPLIES 8

Re: How do I show Customer Groups at Registration

By Default, Magento doesn't provide this type of functionality to display drop-down for the customer group in the registration form. You need to customization of the registration page and add custom drop-down based on customer group to register page.

 

After submitting registration form you need to set Customer group based on users selected value and save programmatically customer group to that customer.

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

Re: How do I show Customer Groups at Registration

Thanks Rakesh for taking time to respond.  I really appreciate it.   If you have the codes for customization, please share it.  You can send it to me in my inbox.  Thanks.

Re: How do I show Customer Groups at Registration

You can follow following steps:

1. Add your phtml to additional info block, by creating following file: /Nano/CommissionAgents/view/frontend/layout/customer_account_create.xml. With contents:

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="customer_groups_additional_info_customer" template="Nano_CommissionAgents::addcustomergroup.phtml"/>
        </referenceContainer>
    </body>
</page>

2. Now create phtml file : /Nano/CommissionAgents/view/frontend/templates/addcustomergroup.phtml. With content :

<?php
$blockObj= $block->getLayout()->createBlock('Nano\CommissionAgents\Block\CustomerGroups');
$groups = $blockObj->getCustomerGroup();
?>
<div class="field group_id required">
    <label for="group_id" class="label"><span><?php /* @escapeNotVerified */ echo __('I want to register as') ?></span></label>
    <div class="control">
        <select name="group_id">
            <?php foreach ($groups as $key => $data) { ?>
            <option value="<?php echo $data['value'] ?>"><?php echo $data['label'] ?>"</option>
            <?php } ?>
        </select>
    </div>
</div>

3. Create a Block to fetch all customer groups that we have to use in phtml. Create block file at (/Nano/CommissionAgents/Block/CustomerGroups.php). With content:

<?php

namespace Nano\CommissionAgents\Block;

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;

Class CustomerGroups extends Template {
    public $_customerGroup;
    public function __construct(
            CustomerGroup $customerGroup
    ) {
        $this->_customerGroup = $customerGroup;
    }
    
    public function getCustomerGroup() {
        $groups = $this->_customerGroup->toOptionArray();
        return $groups;
    }    
}

4. Create an Observer for event "customer_register_success". For this create an events.xml in (/Nano/CommissionAgents/etc/frontend/events.xml). With content:

<?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="Nano\CommissionAgents\Observer\SaveCustomerGroupId" />
    </event>
</config>

5. Create observer class to save customer group after registration in file (/Nano/CommissionAgents/Observer/SaveCustomerGroupId.php). With content:

<?php

namespace Nano\CommissionAgents\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.'));
       }
    }
}

6. Now try registering after: setup-upgrade, cache flush, static-content-deply and check. Hope you will find customer group in frontend and also be able to save that group (can be verified in backend).

 

If Issue Solved or you find solution helpfull, Click Kudos/Accept As solutions.

 

Re: How do I show Customer Groups at Registration

i used this code but  getting blank registration form

Re: How do I show Customer Groups at Registration

Das funktioniert so in etwa, bissl finetuning muss noch dazu.

 

Es fehlt noch die registration.php und die module.xml um das Modul anzumelden.

 

Vielen Dank @narendra_vyas

Hast mir meinen Tag gerettet

Re: How do I show Customer Groups at Registration

It's working on frontend perfect but not updated in the backend in all customer list, let me guide 

Re: How do I show Customer Groups at Registration

This is worked, thank you.  Is there a way to exclude groups for example if you don't want "Not Logged In" and "general" to show in that dropdown list?

Re: How do I show Customer Groups at Registration

I am getting an error group not found