0

I want to get the list of all active offline payment methods like cashondelivery, checkmo, banktransfer etc.

0

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.