Magento 2 – Make Last Name Optional in Customer Registration Form

customerformsmagento2.1.6

I need to make lastname optional in customer registration form.

Best Answer

First of all you should remove the is_required option from the lastname attributes in the customer entity and address entity:

UPDATE `eav_attribute` SET `is_required`=0 WHERE `attribute_code`='lastname'

Then you should rewrite registration form of your theme. In the default magento setup this template is located here:

vendor/magento/module-customer/view/frontend/templates/widget/name.phtml

You should remove validation and required class from the lastname label and lastname input, like:

<div class="field field-name-lastname">
    <label class="label"  for="<?php /* @escapeNotVerified */ echo $block->getFieldId('lastname') ?>">
        <span><?php /* @escapeNotVerified */ echo $block->getStoreLabel('lastname') ?></span>
    </label>

    <div class="control">
        <input type="text" id="<?php /* @escapeNotVerified */ echo $block->getFieldId('lastname') ?>"
               name="<?php /* @escapeNotVerified */ echo $block->getFieldName('lastname') ?>"
               value="<?php echo $block->escapeHtml($block->getObject()->getLastname()) ?>"
               title="<?php /* @escapeNotVerified */ echo $block->getStoreLabel('lastname') ?>"
               class="input-text <?php /* @escapeNotVerified */ echo $block->getAttributeValidationClass('lastname') ?>" <?php /* @escapeNotVerified */ echo $block->getFieldParams() ?>>
    </div>
</div>

Important Note: Do not change the original file! You should do it in a custom module or theme!

Related Topic