I've followed the guide on the selected answer here:
Payment method only admin Magento 2?
But still no luck.
What I want is to be able to show the Cash On Delivery method only to certain customers. The guide above does not remove the payment method.
Thanks all!
Best Answer
We will describe a simple solution based on the Payment Restriction module, where MageWorx – a vendor name and Payment Restriction – a module name.
Then, add a registration file:
A Composer file (if you decide to transfer the module with the ability to install it using the Composer):
A file with the module declaration:
Then, we need to create a
di.xml
file in a common area (this won't work on the Frontend) and declare our plugin in it:Now, the last and main file with the plugin class that is responsible for the check:
For unknown reasons, if you create this plugin only for the frontend, the payment method will be displayed on the checkout page. However, if you try to use it, it will result in an error and return to the checkout. That makes it necessary to validate not only the customer's session but also the admin session. If the admin is logged in, we should not change anything and return the original result.
Also, you can add an additional check on the frontend. For example, you can create a simple module that allows to segment the 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 data.
Here is the check:
The Cash on Delivery method is hidden for the not logged in customer:
The same for the logged in customer:
Visible:
Always visible for a store admin:
Note:
We recommend you to do the same actions in a separate module to have the ability to disable it.
You have to change a vendor and module name to your own.
This solution was created and tested on Magento 2.1.