Reading list Switch to dark mode

    List all Active Payment Methods list Magento 2

    Updated 21 February 2024

    Magento-Code-Snippet-5-3

    Today we are discussing how to get list of all payment methods which are active in Magento2.

    To get this you can use following code:

    <?php
    
    namespace Webkul\Test\Model;
    
    use \Magento\Framework\App\Config\ScopeConfigInterface;
    use \Magento\Payment\Model\Config;
    
    class Paymentmethod extends \Magento\Framework\DataObject 
        implements \Magento\Framework\Option\ArrayInterface
    {
        /**
         * @var ScopeConfigInterface
         */
        protected $_appConfigScopeConfigInterface;
        /**
         * @var Config
         */
        protected $_paymentModelConfig;
        
        /**
         * @param ScopeConfigInterface $appConfigScopeConfigInterface
         * @param Config               $paymentModelConfig
         */
        public function __construct(
            ScopeConfigInterface $appConfigScopeConfigInterface,
            Config $paymentModelConfig
        ) {
    
            $this->_appConfigScopeConfigInterface = $appConfigScopeConfigInterface;
            $this->_paymentModelConfig = $paymentModelConfig;
        }
     
        public function toOptionArray()
        {
            $payments = $this->_paymentModelConfig->getActiveMethods();
            $methods = array();
            foreach ($payments as $paymentCode => $paymentModel) {
                $paymentTitle = $this->_appConfigScopeConfigInterface
                    ->getValue('payment/'.$paymentCode.'/title');
                $methods[$paymentCode] = array(
                    'label' => $paymentTitle,
                    'value' => $paymentCode
                );
            }
            return $methods;
        }
    }

    Here,

    $_appConfigScopeConfigInterface is an object of Magento\Framework\App\Config\ScopeConfigInterface class.

    $_paymentModelConfig is an object of Magento\Payment\Model\Config class.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    and in toOptionArray() function we use a method: getActiveMethods() which returns model of all the active methods.

    In return array we have all payment methods with their code and title.

    I hope this blog will help you with Setup Script files in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • farman
    • Neelesh Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home