Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top