In this tutorial, Today I will explain to how to get payment method information from order data in Magento 2. To get payment method information, you need to inject \Magento\Sales\Api\OrderRepositoryInterface class in your construct.
Let’s see the example to get payment method information.
You may also like this :
1) First of all, Let’s assume that you have created a simple module. Now, you can add the below code in your block/controller file to get payment method information.
<?php
/**
* Created By : Rohan Hapani
*/
namespace RH\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected $orderRepository;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
array $data = []
) {
$this->orderRepository = $orderRepository;
parent::__construct($context, $data);
}
public function getPaymentInfo()
{
$orderId = 10;
$order = $this->orderRepository->get($orderId);
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
echo $method->getTitle()."<br/>"; // Check / Money order
echo $method->getCode()."<br/>"; // checkmo
echo $payment->getShippingAmount()."<br/>"; // 5.0000
echo $payment->getAmountPaid()."<br/>"; // 36.3900
echo $payment->getAmountOrdered()."<br/>"; // 36.3900
}
}
?>
Now, you can use getPaymentInfo() function to get payment information. I display information like payment method title, payment method code, shipping amount from order, the amount paid from order, and amount ordered from order details here. You can get the order object by pass your order id.
That’s it !!!
I hope this blog is easy to understand how to get payment method information from order data in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with a proper solution.
Stay Safe and Stay Connected !!