Magento 2 Success.phtml显示付款方式

时间:2018-05-19 14:24:19

标签: php magento

我需要在Magento 2网站的success.phtml页面中显示付款方式代码或名称。

我创建了模块;我在success.phtml中添加了“您的付款方式是:”句子;但是,无法在这里写下付款方式。

我只想说下面的话:

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

你能给我的模块函数php一个例子来创建paymentmethod()函数吗?

我尝试了stackoverflow中的所有内容

这些链接对我没有帮助:

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

我最近的代码就是这个(什么都不返回):

<?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 个答案:

答案 0 :(得分:0)

试试这个:

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

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

在phtml中:

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

请记住,比覆盖Success类更好的解决方案是创建自定义块并在success.phtml

中呈现它