In this article, you going to see a discussion regarding Magento 2 get payment method title from order in detail and step by step.
If you are looking for this topic then you are at right place.
Let’s start with topic regarding Magento 2 get payment method title from order
With the help of order repository interface you can get payment method title from order details.
Adding an API OrderRepositoryInterface
with Order id, you can load all the order related data and get payment object from it.
Using Payment Object, get all the payment related details.
Below is code snippet from which you can understand how to get payment method title .
<?php
namespace VendorName\ModuleName\PathtoHelper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
parent::__construct($context);
}
/**
* @inheritdoc
*/
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
}
}
By Default, if you check you cannot get all payment related information in order.
One more method to get Payment Method Title.
Using the payment model from the order.
Then in the next step is to get method instance from the payment.
$orderIncrementId=10000003;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId)
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
$methodTitle = $method->getTitle();
In the above code snippet $order
is an instance of Magento\Sales\Model\Order
model
Magento 2 get Order details by Increment Id programmatically
Conclusion :
Finally, we have done with the topic completely.
With the help of above code snippet one can do operation for Magento 2 get payment title from order programmatically.
I hope you like this article and if you have any query, please comment below.