Question

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

Was it helpful?

Solution

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.

OTHER TIPS

As an additional answer for a quick fix.

In your theme create file customer_account_create.xml in Magento_Customer/layout

File contents to be

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="customer_form_register" remove="true"/>

    </body>
</page>

To remove registration link from the login page

Create file customer_account_login.xml in Magento_Customer/layout

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>

        <referenceBlock name="customer.new" remove="true"/>

    </body>

</page>

Then to remove the registration link from the top links bar

Create file default.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="register-link" remove="true"/>

    </body>
</page>

As long as you have recaptcha enabled on customer sign ups, it would not be possible to sign up.

The previous solution didn't worked on magento 2.2.10, the workaround in my case was to edit the file vendor/magento/module-customer/view/frontend/layout/default.xml and comment the block "Magento\Customer\Block\Account\RegisterLink" like this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="top.links">
        <block class="Magento\Customer\Block\Account\Link" name="my-account-link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">My Account</argument>
                <argument name="sortOrder" xsi:type="number">110</argument>
            </arguments>
        </block>
<!--
        <block class="Magento\Customer\Block\Account\RegisterLink" name="register-link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Create an Account</argument>
            </arguments>
        </block>
-->
        <block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link"
               template="Magento_Customer::account/link/authorization.phtml"/>
    </referenceBlock>
...<!--the file continues -->

This will remove the "Create an Account" link on the top menu.

Then to remove the "New Customers" block on the login page I've edited the file vendor/magento/module-customer/view/frontend/layout/customer_account_login.xml and comment the block "Magento\Customer\Block\Form\Login\Info" like this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <title>Customer Login</title>
</head>
<body>
    <referenceContainer name="content">
        <!-- customer.form.login.extra -->
        <container name="customer.login.container" label="Customer Login Container" htmlTag="div" htmlClass="login-container">
            <block class="Magento\Customer\Block\Form\Login" name="customer_form_login" template="Magento_Customer::form/login.phtml">
                <container name="form.additional.info" as="form_additional_info"/>
            </block>
<!--
            <block class="Magento\Customer\Block\Form\Login\Info" name="customer.new" template="Magento_Customer::newcustomer.phtml"/> 
-->
        </container>
<!--the file continues -->

After that it's needed to run this commands as the magento file system owner on the magento installation directory:

  1. bin/magento setup:upgrade
  2. bin/magento setup:di:compile
  3. bin/magento setup:static-content:deploy (with the proper locale)
  4. bin/magento cache:clean

Finally to prevent any user from accessing the url example.com/customer/account/create

I've created a redirection on the server to the home page, in my case with nginx.

location /customer/account/create {
    rewrite ^/customer/account/create https://www.example.com/ permanent;
}

If you are using Enterprise Edition's Company Accounts, there is one more place to block.

app/design/frontend/Vendor/Theme/Magento_Company/layout/default.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="register.link.container" remove="true"/>

    </body>
</page>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top