How to block/remove registration of new customers in Magento 2?
Magento 2 has a system in place to facilitate disabling customer registration. The customer module includes a model of which it's the sole purpose to return a boolean true
or false
to indicate if customer registration is allowed. The "Create an Account" link block, the "New Customers" block template (on the Customer Login page) and the Customer Account Create and CreatePost Controllers consult that model and based on it's return value they do or do not display their content.
This model is \Magento\Customer\Model\Registration
:
namespace Magento\Customer\Model;
class Registration
{
/**
* Check whether customers registration is allowed
* @return bool
*/
public function isAllowed()
{
return true;
}
}
At first, it seems a bit weird to have a whole class that just returns true
, but this is in place to facilitate a single point where the Magento 2 Enterprise module WebsiteRestrictions can hook into to manipulate the returned boolean value based upon the Website Restrictions configuration you can set in the back end of an Enterprise shop.
You can use that very same construction to disable customer registration on your own in your Magento 2 Community Edition, like the module pointed out in the comments (https://github.com/deved-it/magento2-disable-customer-registration) is also doing. Just create an after Plugin on the isAllowed()
method:
app/code/MyStore/Customer/Plugin/Customer/Model/RegistrationPlugin.php
:
namespace MyStore\Customer\Plugin\Customer\Model;
use Magento\Customer\Model\Registration;
class RegistrationPlugin
{
/**
* @param Registration $subject
* @param boolean $result
*/
public function afterIsAllowed(Registration $subject, $result)
{
return false;
}
}
app/code/MyStore/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\Model\Registration">
<plugin name="MyStoreCustomerRegistrationDisable" type="MyStore\Customer\Plugin\Customer\Model\RegistrationPlugin" />
</type>
</config>
Of course you can also introduce a config setting to decide whether to return true
or false
, just like was done in the linked module on GitHub.