1

I'm using Magento 2.2.3 and what I want to achieve is to put a dropdown selection for customer group on the registration page because some of them are resellers. Is this possible and if so, how do we do this?

1

What you're talking about is perfectly possible and the best approach here would be to create a custom module to do so. Without going in super depth, here's a summary of what you'd need to do:

1: Create the basic module structure (see https://devdocs.magento.com/videos/fundamentals/create-a-new-module/)

2: Override the register form block to use a custom version of the register template. To do that you would create a view/frontend/customer_account_create.xml with the following contents:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register" template="Your_Module::register.phtml" />
    </body>
</page>

Then copy the contents of vendor/magento/module-customer/view/frontend/templates/form/register.phtml to /path/to/magento/app/code/Your/Module/view/frontend/templates/register.phtml

3: Add custom html & php to your override of the register.phtml form to show the dropdown of customer groups.

I would recommend using a ViewModel as a mechanism to get the customer group data. So for example, in the layout xml above you would do:

<referenceBlock name="customer_form_register" template="Namespace_Module::register.phtml" >
    <arguments>
        <argument name="view_model" xsi:type="object">Your\Module\ViewModel\CustomerGroups</argument>
    </arguments>
</referenceBlock>

This would then allow you to do the following in your register.phtml:

$viewModel = $block->getViewModel();
$groups = $viewModel->getCustomerGroups();

Your view model should use the inject the Magento\Customer\Model\Api\Data\GroupRepositoryInterface class and use that to get the customer groups and return them as an array. You can then use the array in the template to build the select & options.

Also ensure the name of the select dropdown is set to group_id as this should then feed through to the data processing correctly (havent tested this though)

  • I had trouble on the view model part. Couldn't get it right. – Jim Jul 26 '18 at 16:44
0

you just need to observer to save customer group

override fire for display Group in form and add code

\app\design\frontend\Helloenergie\default\Magento_Customer\templates\form\register.phtml


<div class="field field-name-customer-group required">
            <div class="control switch-field c-group">
                <div class="switch general active">
                    <label class="tab-radio-des radio-des">
                      <?= $block->escapeHtml(__('General')) ?>
                        <input
                        title="<?= __('Customer Group') ?>"
                        id="group_id" 
                        class=""
                        data-validate="{required:true}"
                        name="group_id" type="radio"
                        value="1" checked="checked" />
                    </label>
                </div>
                <div class="switch eu-company">
                    <label class="tab-radio-des radio-des">
                      <?= $block->escapeHtml(__('Reseller')) ?>
                        <input
                        title="<?= __('Customer Group') ?>"
                        id="group_id" 
                        class=""
                        data-validate="{required:true}"
                        name="group_id" type="radio"
                        value="2" />
                    </label>
                 </div>
            </div>
        </div>
  1. Use Event customer_register_success in events.xml

3.Write Observer

<?php 


namespace Xxx\Yyy\Observer;
// added by me
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Vat;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Message\ManagerInterface;



class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface {

    /**
     * @var CustomerRepositoryInterface
     */

    private $customerRepository;
    protected $_addressFactory;
    private $customerSession;
     protected $messageManager;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
         CustomerRepositoryInterface $customerRepository,
         \Magento\Customer\Model\AddressFactory $addressFactory,
         Vat $customerVat,
         CustomerSession $customerSession,
         ManagerInterface $messageManager
        )
    {
        $this->_request = $request;
        $this->customerRepository = $customerRepository;
        $this->_addressFactory = $addressFactory;
        $this->_customerVat = $customerVat;
        $this->customerSession = $customerSession;
        $this->messageManager = $messageManager;
    }
    //ends here
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ){      

        $id = $observer->getEvent()->getCustomer()->getId();
        $customer = $this->customerRepository->getById($id);

        if($group_id = $this->_request->getParam('group_id')){
            $customer->setGroupId($group_id);
            $this->customerRepository->save($customer);
            $this->customerSession->setCustomerGroupId($group_id);
        }


    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.