0

I have created a customer drop-down attribute from admin, in the registration page i need to show it as a radio button, how can achieve this. is it possible to show its as radio button, if not is there any alternative option?

Thanks

1 Answer 1

0

To display a customer dropdown attribute as a radio button on the registration page in Magento 2, you'll need to create a custom module and perform some customization.

Here's a step-by-step guide to achieve this:

1. Create a Custom Module: Create a custom module in the app/code directory of your Magento installation. Let's call it Vendor_CustomerAttribute.

2. Define the Customer Attribute: In your custom module, define the customer attribute. Create the necessary files to set up the attribute:

app/code/Vendor/CustomerAttribute/Setup/InstallData.php app/code/Vendor/CustomerAttribute/Setup/UpgradeData.php

In these files, use Magento's setup scripts to add the customer attribute of the dropdown type. you can take reference from below links :

How to create customer custom attribute for radio buttons, checkbox in magento 2

3. Modify the Registration Form: Create a custom layout XML file in your module to modify the registration form layout. This can be done in a file like app/code/Vendor/CustomerAttribute/view/frontend/layout/customer_account_create.xml.

   <?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">
               <action method="setTemplate">
                   <argument name="template" xsi:type="string">Vendor_CustomerAttribute::form/register.phtml</argument>
               </action>
           </referenceBlock>
       </body>
   </page>

4. Create the Template File: Create a custom template file for the registration form in your module: app/code/Vendor/CustomerAttribute/view/frontend/templates/form/register.phtml.

Inside this template, you can customize the rendering of your custom attribute as radio buttons. Here's an example:

   <?php /* @var $block \Magento\Customer\Block\Form\Register */ ?>
   <?php $_customAttribute = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Gender'); ?>

   <div class="field gender required">
       <label for="gender" class="label"><span><?= $block->escapeHtml(__('Gender')) ?></span></label>
       <div class="control">
           <?= $_customAttribute->setShowAsRadio(true)->toHtml(); ?>
       </div>
   </div>
   

In this example, we're assuming that the customer attribute code is "gender". You should replace it with your actual attribute code.

5. Clear Cache: After making these changes, clear the cache and test the registration form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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