Get Payment Method on Success Page Programmatically in Magento 2

magento2order-success-page

How can I display the payment method in the success page using a code?
If anyone can help me, it would be great. Thankyou.

Best Answer

Copy the success.phtml from:

vendor/magento/module-checkout/view/frontend/templates/success.phtml

to your theme location:

app/design/frontend/Vendor/theme/Magento_Checkout/templates/success.phtml

Now add the following code to your success.phtml

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();       
$payment = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId())->getPayment();
echo $paymentMethodTitle = $payment->getMethodInstance()->getTitle();
?>

You can also create one block which extends Success block and then create one method which should return the above code.

Note: Use of Objectmanager directly in phtml file is not a good practice. you need to create block and adjust those according to your requirement.

Hope this helps!

Related Topic