8

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

2
17

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.

7
  • This solution is not working for magento 2.2.10, for what is worth neither the github module
    – Typo
    Jan 21 '20 at 14:05
  • This was 3 years ago on Magento 2.1. Please post back here if you find a solution for Magento 2.2 (2.2.10).
    – 7ochem
    Jan 21 '20 at 15:06
  • I know, It's just a clarification for new readers.
    – Typo
    Jan 21 '20 at 15:13
  • I've found a CustomerAuthorization class in vendor/magento/Customer/Model/Plugin that contains a function called "aroundIsAllowed" that looks like it's enforcing the use of EE's api, I'm not sure though, but I'll bet that adobe is taking care of business.
    – Typo
    Jan 22 '20 at 15:57
  • 1
    Not sure why people say, this is not working anymore. It's working perfectly in Magento 2.4
    – zlep
    Apr 7 at 15:51
2

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.

5
  • you should mention on which Magento version were you working on.
    – Typo
    Feb 5 at 20:51
  • Poster just said M2... Anyway, 2.2 - 2.4 should be fine. But tested on 2.3 - 2.4. 2.2 was just terrible branch of M2 and hopefully everyone has upgraded to 2.3 now.
    – elfling
    Feb 5 at 21:54
  • The tag is magento 2.1, so you didn't tested on 2.2, but you're claiming that it works and although my solution was tested on 2.2.10 your comment on my answer implies that yours is a solution that works on 2.2.
    – Typo
    Feb 6 at 13:47
  • 1
    I wouldn't get too hung on it. Life goes on and people need up to date answers. Moving on
    – elfling
    Feb 6 at 23:42
  • Learn your manners boy.
    – Typo
    Feb 7 at 3:33
1

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;
}
5
  • Thanks. I was looking for something like this. Does this still work for 2.4?
    – zlep
    Nov 25 '20 at 13:00
  • @zlep I didn't test it on 2.4
    – Typo
    Nov 25 '20 at 16:17
  • To remove that block, you just need to do <referenceBlock name="customer.new" remove="true"/>
    – elfling
    Feb 2 at 17:54
  • @elfling if you have a better answer feel free to post it.
    – Typo
    Feb 4 at 3:10
  • @Typo - No problem, I've added another solution to this answer for a quick fix version
    – elfling
    Feb 4 at 14:29
1

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>

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.