4

I need to make lastname optional in customer registration form.

2 Answers 2

8

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!

7
  • Hi Siarhey Uchukhlebau, thanks for quick reply. it works fine, but i am importing customers from m1 to m2, while importing some of the customers are not imported, why because customer last name was empty. But your solution works fine in frontend, any idea to fix while importing data.
    – balu
    May 31, 2017 at 13:02
  • @balu Which script you are using ? May 31, 2017 at 13:04
  • one paid extension : magento2magento2_v3.0.2 , by using this extension we are importing data from magento 1 to magento 2.
    – balu
    May 31, 2017 at 13:06
  • @balu Sorry, but I cant help you without source code :( May 31, 2017 at 13:51
  • No problem, Thank you very much , your solution helpful in register account and address edit scenaries.
    – balu
    May 31, 2017 at 14:08
8

I think that it would be better to update the

is_required

option through upgrade script instead of run a query directly on database.

You should create in your module an UpgradeData.php in this path

app/code/Vendor/Module/Setup

and put inside this code

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    )
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup,
                            ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1', '<=')) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->updateAttribute(Customer::ENTITY, 'lastname', 'is_required', 0);
        }

        $setup->endSetup();
    }
}

Then run bin/magento setup:upgrade to apply to database

2
  • This should be the accepted answer
    – omsta
    Feb 28, 2019 at 16:33
  • This updates the DB Entry, but does not pass through the JS validation in admin panel.
    – Harish ST
    Aug 14, 2021 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.