0

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

2
  • What is the exact magento 2 version you are using? – Sukumar Gorai Jan 3 '19 at 11:15
  • @SukumarGorai I am using Magento ver. 2.1.7 – Meera Jan 3 '19 at 11:17
2

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!

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.