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/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;
}
}
public function getPaymentMethod() {doesn't usereturn. See http://php.net/manual/pl/functions.returning-values.php - krubo