1
votes

I need to show payment method code or name in success.phtml page of my Magento 2 site.

I create the module; and i add 'Your payment method is:' sentence in my success.phtml; but, couldn't write the payment method here.

I just want to say something like below:

<?= __('Your order method is:',$block->getPaymentMethod()) ?>

Could you give an example for my modules function php to create paymentmethod() function?

I tried everything in stackoverflow

These links didnt help me:

https://magento.stackexchange.com/questions/153437/magento-2-payment-method-code-on-order-success-page

https://magento.stackexchange.com/questions/156933/get-payment-method-title-of-an-order

My recent code is this (returns nothing):

<?php

namespace MyFirm\Ordersuccess\Block\Checkout;

use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\OrderFactory;

class Success extends \Magento\Checkout\Block\Success {

protected $checkoutSession;
protected $_customerSession;

public function __construct(
    Context $context,
    OrderFactory $orderFactory,
    Session $session,
    array $data = []
) {
    $this->checkoutSession = $session;
    parent::__construct($context, $orderFactory, $data);
}

public function getPaymentMethod() {

    $payment = $this->checkoutSession->getLastRealOrder()->getPayment();
return $payment;
}

}
1
Your public function getPaymentMethod() { doesn't use return. See http://php.net/manual/pl/functions.returning-values.php - krubo
you are definately right :) i fix it as like in question; but still not working - stackers_alti

1 Answers

1
votes

Try this:

public function getPaymentMethod() {
    $payment = $this->checkoutSession->getLastRealOrder()->getPayment()->getMethod();

    // You can stop debbuger here to be sure what is $payment.
    return $payment;
}

And in phtml:

<?php echo __('Your order method is: ') . $block->getPaymentMethod() ?>

Keep in mind that, better solution than overriding Success class is creating a custom block and render it in success.phtml