3

I have a Magento 2.15 site that almost exclusively sells B2B. We don't have many customers that are just individuals. 99% of them are businesses.

So when checking out, the people that order for their business understandably don't want to put a first and last name in for the billing or shipping address. Some of them just type "none" for both boxes and then proceed to put the company name in the company name box.

How can I make the First and Last name fields optional? Will this possibly have rammifications elsewhere? I know Magento 2 is weird about addresses as I've had issues with default addresses causing errors in the past.

TL:DR How do I make "First Name" and "Last Name" fields optional when entering both shipping and billing addresses? It should also be done on any other places where an address is entered, like registration.

Thanks

1 Answer 1

5

Here's a simple method that can be used to make any required address field optional.

First create a plugin for Magento\Checkout\Block\Checkout\LayoutProcessor. Define the class as follow:

<?php


namespace Vendor\Module\Plugin;

class LayoutProcessor
{

    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        $jsLayout
    ) {
        // Make fields not required
        
        $nameLayout = [
            'validation' => [
                'required_entry' => false
            ]
        ];

        // Change in shipping address

        $firstnameField = &$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
                          ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname'];
        $lastnameField = &$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
                         ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['lastname'];

        $firstnameField = array_merge($firstnameField, $nameLayout);

        $lastnameField = array_merge($lastnameField, $nameLayout);

        // Change in billing address

        foreach ($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                 ['payment']['children']['payments-list']['children'] as &$child)
        {
            if (isset($child['children']['form-fields'])) {
                $child['children']['form-fields']['children']['firstname'] =
                    array_merge($child['children']['form-fields']['children']['firstname'], $nameLayout);
                $child['children']['form-fields']['children']['lastname'] =
                    array_merge($child['children']['form-fields']['children']['lastname'], $nameLayout);
            }
        }

        return $jsLayout;
    }
}

All this does is change the first and last name fields to optional in the checkout address forms. However, if the fields are left blank, the various address verifications that occur on the server-side will fail so there's one more step to get around the verification.

We'll make a plugin for the getFirstname and getLastname methods of Magento\Quote\Model\Quote\Address as follows:

<?php


namespace Vendor\Module\Plugin;

class Address
{
    public function afterGetFirstname(
        \Magento\Quote\Model\Quote\Address $subject,
        $result
    ) {
        if (empty(trim($result))) {
            return "N/A";
        }

        return $result;
    }

    public function afterGetLastname(
        \Magento\Quote\Model\Quote\Address $subject,
        $result
    ) {
        if (empty(trim($result))) {
            return "N/A";
        }

        return $result;
    }
}

In this plugin we check if the address has a first and last name associated with it. If not then we emit a placeholder value to satisfy the verification. This is much simpler than having to override all the classes involved in address verification.

With these two plugins in place, the first and last name fields in checkout will be optional.

2
  • Hi Aaron, This helps me a lot. I want one more favor instead of "N/A" I want to set a NULL value.
    – user41818
    Commented Feb 17, 2018 at 7:09
  • In that case you might need to override all the address verification code which is no small task because it exists in many different files. Or alternatively you might be able to write an event listener for before the entity gets saved and change "n/a" to null at that point. I'm just speculating here. Commented Feb 17, 2018 at 10:23

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.