Since Magento’s core modules lack the ability to remove create account functionality, we must build a unique store configuration in order to make this dynamic and user-friendly for everyone. So, first start with creating system.xml for creating a store configuration.
Create system.xml File:
In your custom module directory (app\code\Vendor\Module), create a etc\adminhtml\system.xml file. If the etc\adminhtml directory does not exist, create it. This file defines the structure of your system configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="customer"> <group id="create_account" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <field id="disable_account_registration" translate="label" type="select" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Disable Account Registration</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> </group> </section> </system> </config> |
Create config.xml File:
In your custom module directory (app\code\Vendor\Module), create a etc\config.xml file. If the etc directory does not exist, create it. This file define the default value of your configuration
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <customer> <create_account> <disable_account_registration>0</disable_account_registration> </create_account> </customer> </default> </config> |
Create Provider.php File:
In your custom module directory (app\code\Vendor\Module), create a Model\Config\Provider.php file. If the Model\Config directory does not exist, create it. This file is use to fetch system configuration value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<?php declare(strict_types=1); namespace Vendor\Module\Model\Config; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\Store\Model\ScopeInterface; /** * Class Provider to fetch config value */ class Provider { private const XML_DISABLE_ACCOUNT_REGISTRATION = 'customer/create_account/disable_account_registration'; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var StoreManagerInterface */ private $storeManager; /** * Provider Constructor * * @param ScopeConfigInterface $scopeConfig * @param StoreManagerInterface $storeManager */ public function __construct( ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager ) { $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; } /** * Get disable account registration Status from configuration * * @return bool */ public function disableAccountRegistration(): bool { return $this->scopeConfig->isSetFlag( self::XML_DISABLE_ACCOUNT_REGISTRATION, ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getId() ); } } |
Create di.xml File:
In your custom module directory (app\code\Vendor\Module), create a etc\frontend\di.xml file. If the etc\frontend directory does not exist, create it. This file is used now to fetch to configure plugin
1 2 3 4 5 6 |
<?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\Model\Registration"> <plugin name="disable_create_account_plugin" type="Vendor\Module\Plugin\Model\RegistrationPlugin"/> </type> </config> |
Create RegistrationPlugin.php File:
Now, we need to create an after plugin for the
Magento\Customer\Model\Registration::isAllowed() function. As we can see this function always returns true. In case to remove create account functionality we need to return false when our condition matches.
In your custom module directory (app\code\Vendor\Module), create a Plugin\Model\RegistrationPlugin.php file. If the Plugin\Model directory does not exist, create it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php declare(strict_types=1); namespace Vendor\Module\Plugin\Model; use Magento\Customer\Model\Registration; use Vendor\Module\Model\Config\Provider; /** * Disable create account feature */ class RegistrationPlugin { /** * @var Provider */ private $configProvider; /** * RegistrationPlugin Constructor * * @param Provider $configProvider */ public function __construct( Provider $configProvider ) { $this->configProvider = $configProvider; } /** * Disable customers registration * * @return bool */ public function afterIsAllowed(Registration $subject, bool $result) { if ($this->configProvider->disableAccountRegistration()) { return false; } return $result; } } |
Run the below commands:
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:di:compile:deploy php bin/magento setup:static-content |
After this, in Magento Admin navigate to Stores -> Configuration -> Customers -> Customer Configuration -> Create New Account Options -> Disable Account Registration and set the value to Yes.
At last Run
1 |
php bin/magento cache:flush |
Now you see the result on Frontend
Result Before:
Result After:
Note: If you want this for a specific website or store. Update this configuration after changing scope.
Shashi Kant
2024-01-08