Get FREE $25 Hosting for Managed Magento Hosting. Use Coupon: MGT25
Avail Now

Magento 2: Hide Add to Cart Button for Guest Users

Magento 2 has become the yardstick for ecommerce platforms because of the multitude of the awesome features and great user experience of the platform.

By default, on Magento 2 powered store, Add to Cart button is always available for guest users. However, store owners have different requirements and there is always the case where the store owners do not wish the visibility of the button to guest users.

In this tutorial, I am going to show you how you could easily disable or hide Add to Cart button in Magento 2 for guest users. Following the best Magento code development practices, I will start by creating a custom module.

Create the Module

Begin by configuring and registering the module. Create module.xml in app/code/Magenticians/Mymodule/etc and add this code to the file:

Next, create registration.php in app/code/Magenticians/Mymodule and add this code to the file

At this point, I can direct use Object Manager in the module addtocart.phtml to check the session of the user. However, this is not the right approach because it wastes too many server resources on unnecessary calls. I did some research and got inspired by Frank Clark’s Guide

Create Event and Observer

Before going further, I think it is important to understand events and observers in Magento 2:

Magento 2 Events

When a specific action is triggered, the event is dispatched by Magento modules in order to pass data to the relevant Observer configured for the dispatched event.

Magento 2 Observers

Observers are used to catch the action which was triggered from events. In observers, you can set the required functionality or logic that is to be executed in response.

To learn more about Magento 2 Events and Observers, check out the DevDocs documentation.

Now, I will create an event which will be triggered before the layout is loaded. This will help in checking the session of the customer. To do this, create events.xml in app/code/Magenticians/Mymodule/etc/frontend and add this code:

Next, create AddHandles.php in app/code/Magenticians/Mymodule/Observer and add this code to the file:

In the above code, you can see the constructor where I have added customer session. Then, in the execute() function, I added the handle customer_logged_out to the layout. To use this handle, create a layout file customer_logged_out.xml in app/code/Magenticians/Mymodule/view/frontend/layout and add this code to it:

Create the phtml File

In the customer_logged_out.xml, I have set the path of addtocart.phtml file for guest users. So, you have to create addtocart.phtml in app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view and add this code to the file:

Run CLI Commands

Launch the SSH terminal and run the following commands in the root directory of your store:

Check the Result

It is time to check the results. Go to any product frontend page as a guest user and you will see that Add to Cart button is not visible.

hide add to cart button magento 2 for guest users

Final Words

I believe that you are now able to hide Add to Cart button in Magento 2 from guest users. If you are facing any issue related to this tutorial, just leave your comment below and I will get back to you.

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.