Retrieve All Payment Methods list in Magento 2.

In Magento 2 You can retrieve all the available payment methods by Magento\Payment\Model\Config\Source\Allmethods class.

Using Allmethods class you can get all the payment methods in the system whether its enable or disable in the admin setting.

You can get only the active payment methods by Get all active payment methods in Magento 2.

List of all the payment methods in Magento 2 getting by below code snippets, Continue reading “Retrieve All Payment Methods list in Magento 2.”

How to Get Payment Method Title/Code from order in Magento 2?

You can get the payment method title from the Order in Magento 2 by the order repository interface.

Load an Order by API OrderRepositoryInterface with Order ID, Get Payment Object, and fetch Payment-related stuff from the Payment Data.

You can get the payment method title of an order by below code snippets below,

<?php
namespace Rbj\Order\Model;

use Magento\Sales\Api\OrderRepositoryInterface;

class Data
{
    public function __construct(
        private readonly OrderRepositoryInterface $orderRepository
    ) {
    }

    public function getPaymentData()
    {
        $orderId = 1;
        $order = $this->orderRepository->get($orderId);
        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        echo $method->getTitle(); // Cash On Delivery
        echo $method->getCode(); // cashondelivery
    }

}

Using the above method you can get any payment method title or code.