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?


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">
        <referenceBlock name="customer_form_register" template="Your_Module::register.phtml" />

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" >
        <argument name="view_model" xsi:type="object">Your\Module\ViewModel\CustomerGroups</argument>

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

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.