Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Remove First Name and Last Name Required Validation from The Magento 2 Checkout Page

Vinh Jacker | 11-11-2024

How to Remove First Name and Last Name Required Validation from The Magento 2 Checkout Page

Optimizing the checkout process is crucial for enhancing user experience and boosting conversion rates. One often overlooked aspect of this process is handling customer names. Although Magento 2 provides extensive customization options for merchants, certain default settings, such as requiring first and last names during checkout, may not be necessary, especially for anonymous purchases or guest checkouts.

In this guide, we’ll explore the steps to remove the first name and last name required validation from the Magento 2 checkout page, simplifying the process for both merchants and customers.

How to Remove First Name and Last Name Required Validation from The Magento 2 Checkout Page

Step 1: Create a custom module

First, you need to create a custom module in Magento. You can achieve this by creating the 3 files below:

  • Create a module directory located in app/code/YourVendorName/YourModuleName

  • Create a registration.php file by using the following command:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'YourVendorName_YourModuleName',
    __DIR__
);
  • Create the module.xml file in app/code/YourVendorName/YourModuleName/etc with the specified code :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="YourVendorName_YourModuleName" setup_version="1.0.0"/>
</config>

Step 2: Remove the required constraint

Method 1: Override file layout in Magento

Create a layout file to override the default Magento layout, thereby removing the requirement for inputting the first and last name.

To do this, make a checkout_index_index.xml file in the app/code/YourVendorName/YourModuleName/view/frontend/layout directory.

Please run the below code:

<?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="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shipping-address-fieldset" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <!-- Update first name field validation -->
                                                                <item name="firstname" xsi:type="array">
                                                                    <item name="validation" xsi:type="array">
                                                                        <item name="required-entry" xsi:type="boolean">false</item>
                                                                    </item>
                                                                </item>
                                                                <!-- Update last name field validation -->
                                                                <item name="lastname" xsi:type="array">
                                                                    <item name="validation" xsi:type="array">
                                                                        <item name="required-entry" xsi:type="boolean">false</item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                     </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Method 2: Create a di.xml file

In the app/code/YourVendorName/YourModuleName/etc, create a di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Checkout\LayoutProcessor" type="YourVendorName\YourModuleName\Block\Checkout\LayoutProcessor"/>
</config>

Next, create a class name LayoutProcessor.php in app/code/YourVendorName/YourModuleName/Block/Checkout

<?php
namespace YourVendorName\YourModuleName\Block\Checkout;

class LayoutProcessor extends \Magento\Checkout\Block\Checkout\LayoutProcessor
{
    /**
     * @param array $jsLayout
     * @return array
     */
    public function process($jsLayout)
    {
        $jsLayout = parent::process($jsLayout);
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname']['validation']['required-entry'] = false;
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['lastname']['validation']['required-entry'] = false;


  return $jsLayout;
    }
}

Step 3: Install the module and clear the cache

Run the following commands in the terminal or SSH:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static:deploy -f
php bin/magento cache:flush

After completing the above steps, entering the first and last name will no longer be required during the Magento checkout process.

Wrap Up

In conclusion, by removing the first and last names required for validation from the Magento 2 checkout page, merchants can create a smoother way to purchase for their customers. We hope that this straightforward and easy-to-follow process will help to remove the required validation for first and last name from the checkout page easily. If you have any issues with following this guide, please don’t hesitate to ask for help.

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services