3

I want to disable the possibility to register as a new customer on the frontend of my Magento 2 Community Edition store (new customers are being created through a different process).

I think I could remove the "Create an Account" link from the top menu and remove the "New Customers" block on the right of the Customer Login page, but I'm looking for a more tight blocking of this functionality. I assume you could still enter the example.com/customer/account/create/ URL manually and see the registration form.


enter image description here

9

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.

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.