Question

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

Was it helpful?

Solution

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!

OTHER TIPS

As pointed out above using the object manager is the incorrect way to achieve this in Magento 2. To achieve this you need to create a module:

MyModule/CustomerPaymentType

(Module can we whatever you want) You then need to extend the \Magento\Checkout\Block\Onepage\Success block inside of your own module and for consistency add it to:

MyModule/CustomerPaymentType/Block/Checkout/Onepage/Success.php

You then add the dependancies to this. Here is an example of the extended block:

<?php

namespace MyModule\CustomerPaymentType\Block\Checkout\Onepage;

class Success
extends \Magento\Checkout\Block\Onepage\Success
{
    protected $order;

    public function __construct(
        \Magento\Sales\Model\Order $order
    ) {
        $this->order = $order;
    }

    public function getOrder(){
       $orderTitle = $this->order->getTitle();
    }
}

Then inside of your:

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

You can now call $block->getOrder()

If you get:

Type Error occurred when creating object: MyModule/CustomerPaymentType\Block\Checkout\Onepage\Success\Interceptor

Don't forget to clear your Generated folder

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top