3

enter image description here

I want the Date of Birth to be edited only once then disable it. Same as photo. Thanks

enter image description here

Nam Cong
  • 49
  • 5

1 Answers1

3

Besides disabling the Dob field from frontend, we also have to disable it in the backend to prevent customers from bypassing the disable feature from frontend.

Let's create a custom module. Assume we set the vendor as Vendor and the module name is Customer.

Step 1: Create registration.php file:
File path: app/code/Vendor/Customer/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Customer', DIR);

Step 2: Create the module.xml file:
File path: app/code/Vendor/Customer/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Customer">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Step 3: Create the di.xml file:
File path: app/code/Vendor/Customer/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Block\Widget\Dob">
        <plugin name="DisableChangeDobInCaseItAlreadyHasValue" type="Magetu\Catalog\Plugin\DisableChangeDobInCaseItAlreadyHasValue"/>
    </type>
</config>

Step 4: Create the DisableChangeDobInCaseItAlreadyHasValue.php plugin file:
File path: app/code/Vendor/Customer/Plugin/DisableChangeDobInCaseItAlreadyHasValue.php

<?php
declare(strict_types=1);

namespace Vendor\Customer\Plugin;

use Magento\Customer\Block\Widget\Dob; use Magento\Framework\Exception\NoSuchEntityException;

class DisableChangeDobInCaseItAlreadyHasValue { /** * Add disabled attribute to Dob field in case Customer already set the Dob value. * * @param Dob $subject * @param string $result * @return string */ public function afterGetHtmlExtraParams(Dob $subject, string $result): string { try { if ($subject->getRequest()->getActionName() !== 'create' && $subject->getData('value')) { $result .= ' disabled'; } } catch (NoSuchEntityException $e) { // do nothing }

    return $result;
}

}

Step 5: Create the events.xml file:
File path: app/code/Vendor/Customer/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_save_before">
        <observer name="RevertDobInCaseItAlreadyHasValue" instance="Vendor\Customer\Observer\RevertDobInCaseItAlreadyHasValue" />
    </event>
</config>

Step 6: Create the RevertDobInCaseItAlreadyHasValue.php observer file:
File path: app/code/Vendor/Customer/Observer/RevertDobInCaseItAlreadyHasValue.php

<?php
declare(strict_types=1);

namespace Vendor\Customer\Observer;

use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface;

/**

  • Revert Dob value in case Customer already set the Dob value before.

/ class RevertDobInCaseItAlreadyHasValue implements ObserverInterface { /* * Revert Dob value in case Customer already set the Dob value before. * * @param Observer $observer * @return $this / public function execute(Observer $observer) { /* @var $customer \Magento\Customer\Model\Customer */ $customer = $observer->getEvent()->getCustomer();

    if ($customer-&gt;getOrigData('dob')
        &amp;&amp; $customer-&gt;getData('dob') !== $customer-&gt;getOrigData('dob')
    ) {
        $customer-&gt;setData('dob', $customer-&gt;getOrigData('dob'));
    }

    return $this;
}

}

Step 7: Deploy the changes:

bin/magento setup:upgrade
bin/magento setup:di:compile

If you are in the production mode, you'll need to deploy static content.

You're done.

Tu Van
  • 6,868
  • 2
  • 11
  • 22