Tweet about this on TwitterShare on Facebook0Share on Google+8Share on LinkedIn14Email this to someoneBuffer this page

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, where MageWorx is a vendor name and Payment Restriction is the name of the module.

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.

Should you have comments questions on the topic, feel free to leave a comment, I’ll be happy to discuss that with you.

Tweet about this on TwitterShare on Facebook0Share on Google+8Share on LinkedIn14Email this to someoneBuffer this page