0

I have a requirement to remove firstname, lastname as required field from registration page which I can achieve by modifying eav_attribute table directly

UPDATE 'eav_attribute' SET 'is_required' = 0 WHERE 'entity_type_id' = 1 && 'attribute_code' = 'lastname';

(Similarly I'll update firstname and remove firstname, lastname input field from the phtml file.)

I know modifying the core tables directly is not a good practice. What are alternatives through which I can achieve the above stated requirement?

After reading Modify core tables vs custom tables and joins, I see it's recommended to create a custom table and use joins. Can someone share any example how that is done ?

1
+50

You should create Install/Upgrade data class and use EavSetup. Here you have an exemple snippet:

class UpgradeData implements UpgradeDataInterface
{

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
    $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    if ($context->getVersion()
        && version_compare($context->getVersion(), '1.0.1') < 0
    ) {
        $this->updateFirstnameAttribute($eavSetup);
    }
    }

    /**
     * @param $eavSetup
     *
     * @return void
     */
    protected function updateFirstnameAttribute(EavSetup $eavSetup)
    {
    $eavSetup->updateAttribute(\Magento\Customer\Model\Customer::ENTITY, 'firstname', 'is_required', false);
    }
}

Of course you can create data provider for new attributes and do it your own way, this is just an example of possible solution for a single attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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