Magento 2 – get all payment methods
In this tutorial I show you how to get all payment methods for Magento 2. This is need if you are working on a custom site with dynamic information on currently available payment methods for this store and country.
Magento 2 – get all payment methods
Magento 2 offers you to use nearly every possible payment method. It comes with a small number of methods that only needs to be configured. You are able to install additional modules from payment providers to allow credit card, bank payment, pay by delivery, pay by invoice, bitcoins, and many more.
Example one
The base class for you is scope. So you need to create a new class that injects
1 | \Magento\Framework\App\Config\ScopeConfigInterface $scope |
From this scope you can get needed information. On type is payment, so you will get a list of possible payment methods by calling:
1 | $paymentMethods = $this->scope->getValue('payment'); |
You can do various things with a list of payment methods. It is possible to simply print it out for customer information.
1 2 3 4 | foreach( $paymentMethods as $key => $value ) { //TODO } |
Example two
Another source for payment methods is a helper class from Magento payment module. For this you need to inject it into one of your classes:
1 2 3 4 5 6 7 8 | protected $paymentHelper; // inject payment helper public function __construct( \Magento\Payment\Helper\Data $paymentHelper ) { $this->paymentHelper = $paymentHelper; } |
You get the needed list by calling
1 | $this->paymentHelper->getPaymentMethods(); |
or with following code
1 | $this->paymentHelper->getPaymentMethodList(); |
Using a helper from aMagento 2 core module is a good idea. Sometimes other modules overwrite its function so you may get a wrong result. For this you can also use another source. Simply try which solution better works for you.
Conclusion
As you can see it is quite easy to get all payment methods in Magento 2. You only need to write your own class for your custom module and inject the right class. You may print this list of all possible methods or do some special checks with it.
Why do you need to get all payment methods? Do you know other useful methods?