Easy Way to Remove Payment Method in Magento 2

0
1374
How to Remove Payment in Magento 2 | MageWorx Magento Blog
Reading Time: 3 minutes

Quite often you may want to display a certain payment method to a particular group of customers while hiding it from the other ones.

Here is a step-by-step instruction on how to quickly implement that in Magento 2.

The solution is based on the Payment Restriction module, the code of which is available below, where MageWorx is a vendor name and Payment Restriction is the name of the module.

How to disable payment method in Magento 2?

1. At the first step you need to create a directory for the module:

2. When done, you may add a registration file.

the Composer file (just in case you may want to transfer the module with the ability to install it via Composer):

and a file needed for the declaration of the module:

At the next step, I’m going to create a di.xml file in a sharing space (note if the file is declared in the frontend scope, you won’t get the desired result) and declare our plugin there:

Now, let’s add the last and the main file with the plugin class that is responsible for the check:

IMPORTANT! 

Please note that for some reason, the plugin created only for the frontend won’t let you delete the chosen payment method – it will anyway be shown on the checkout page. However, if a customer tries to choose it, that will trigger an error and throw a customer back to the checkout step.

That’s why to prevent this, it’s necessary to validate not only the customer’s session, but also the session of the admin. If you’re logged in as an admin, don’t change anything to get the original result.

Also, you can add an extra check for the frontend. That can be done by developing a simple module that segments the available payment methods according to customer groups.

Moreover, you can disable certain payment methods not only for the selected countries (the default option) but also for streets, phone numbers or any other location criteria.

Below is an example:

  1. The Cash on Delivery method is hidden for the not logged in customer:1
  2. The same for the logged in customer:IjcdJ
  3. Visible:5H9o3
  4. Always visible for a store Admin:

l6lgz

Some Extra Recommendations

  1. Implement the same activities in a separate module to get an ability to disable it.
  2. Change a vendor and a module name to your own.

This solution was created and tested on Magento 2.1.


Still ‘ve got troubles removing a payment method in Magento 2? Feel free to leave a comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here