5

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

14

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.

| improve this answer | |
  • This solution is not working for magento 2.2.10, for what is worth neither the github module – Typo Jan 21 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 at 15:06
  • I know, It's just a clarification for new readers. – Typo Jan 21 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 at 15:57
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;
}
| improve this answer | |

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.