I need to make lastname optional in customer registration form.
2 Answers
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!
-
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.– baluMay 31, 2017 at 13:02
-
-
one paid extension : magento2magento2_v3.0.2 , by using this extension we are importing data from magento 1 to magento 2.– baluMay 31, 2017 at 13:06
-
-
No problem, Thank you very much , your solution helpful in register account and address edit scenaries.– baluMay 31, 2017 at 14:08
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
-
-
This updates the DB Entry, but does not pass through the JS validation in admin panel. Aug 14, 2021 at 16:12