Magento 2 – Change Customer Name Prefix to Radio Button on Checkout Page

checkoutcustomermagento2

I want to change name prefix to radio button in billing and shipping address.
How can I do that possible?
I want to change this in Customer pages also.

enter image description here

Best Answer

For checkout:

Step 1: VendorName/ModuleName/etc/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="sr_ModuleName_checkout_layout_processor" type="VendorName\ModuleName\Plugin\Block\LayoutProcessor" sortOrder="1"/>
    </type>
</config>

Step 2: VendorName/ModuleName/Plugin/Block/LayoutProcessor.php


namespace VendorName\ModuleName\Plugin\Block;

class LayoutProcessor
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        if(isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset'])) {

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['prefix']['component'] = "Magento_Ui/js/form/element/checkbox-set";

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['prefix']['config']['elementTmpl'] = "ui/form/element/checkbox-set";

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['prefix']['config']['multiple'] = false;
        }

        if(isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list'])) {
            $paymentsList = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list']['children'];

            foreach ($paymentsList as $paymentCode => $payment) {
                if(strpos($paymentCode, '-form') === false) {
                    continue;
                }
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$paymentCode]['children']['form-fields']['children']['prefix']['component'] = "Magento_Ui/js/form/element/checkbox-set";
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$paymentCode]['children']['form-fields']['children']['prefix']['config']['elementTmpl'] = "ui/form/element/checkbox-set";
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$paymentCode]['children']['form-fields']['children']['prefix']['config']['multiple'] = false;
            }
        }
        if(isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['billing-address-form'])) {
            $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children']['prefix']['component'] = "Magento_Ui/js/form/element/checkbox-set";

            $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children']['prefix']['config']['elementTmpl'] = "ui/form/element/checkbox-set";

            $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children']['prefix']['config']['multiple'] = false;
        }

        return $jsLayout;
    }
}

For customer edit page:

Overwrite vendor/magento/module-customer/view/frontend/templates/widget/name.phtml template inside your module or theme. Change prefix dropdown to checkbox.

Related Topic