We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Get The Payment Method Title of an Order in Magento 2?
By using order repository interface, you can easily get the Payment Method Title of an order from the order details in Magento 2.
How can you do it? Here’s how
- Firstly, load an Order by API OrderRepositoryInterface with Order id,
- Secondly, get Payment Object, and
- Lastly, fetch payment related stuff from the Payment Data.
If you’re still confused or lost, don’t worry. The below written code snippets will help you out thoroughly:
<?php
namespace MageSpark\PaymentMethod\Helper;
/**
* Class Data
*
* @package MageSpark\PaymentMethod\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected $_orderRepository;
/**
* Data constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
$this->_orderRepository = $orderRepository;
parent::__construct($context);
}
/**
* Return payment method title for specific order Id
*/
public function getPaymentData()
{
$orderId = 1;
$order = $this->_orderRepository->get($orderId);
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
echo $method->getTitle(); // Check / Money order
echo $method->getCode(); // checkmo
}
}