I want to get the list of all active offline payment methods like cashondelivery, checkmo, banktransfer etc.
- did you get any way to list out all active offline payment methods?Mohit Kumar Arora– Mohit Kumar Arora2018-07-09 11:24:40 +00:00Commented Jul 9, 2018 at 11:24
- just try with this blog.chapagain.com.np/…Nagaraju Kasa– Nagaraju Kasa2018-11-19 06:54:58 +00:00Commented Nov 19, 2018 at 6:54
1 Answer
Although the question is very old, but since it has not been answered yet, I would like to answer.
I assume that we need to get the active offline payment methods in a controller file and the controller has been defined in the routes.xml already.
To get the active offline payment methods, we need to first inject the \Magento\Payment\Helper\Data class in the constructor and then use the getPaymentMethods() function to get all payment methods.
After this the class should look something like this:
<?php namespace {Package}\{Module}\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $_paymentHelper; public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Payment\Helper\Data $paymentHelper) { $this->_paymentHelper = $paymentHelper; parent::__construct($context); } public function execute() { $paymentMethods = $this->_paymentHelper->getPaymentMethods(); if (count($paymentMethods)) { foreach ($paymentMethods as $paymentMethod) { if (isset($paymentMethod['active']) && $paymentMethod['active'] == 1 && isset($paymentMethod['group']) && $paymentMethod['group'] == 'offline') { echo "<pre>"; print_r($paymentMethod); echo "</pre>"; } } } } } I hope, this should help.
Help Source: http://blog.chapagain.com.np/magento-2-get-payment-methods-all-active-used/