Magento 2 Success.phtml 显示付款方式



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

我创建了模块;我在我的success.phtml中添加了"您的付款方式是:"句子;但是,无法在此处编写付款方式。

我只想说下面这样的话:

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

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

我在堆栈溢出中尝试了所有内容

这些链接对我没有帮助:

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 MyFirmOrdersuccessBlockCheckout;
use MagentoCheckoutModelSession;
use MagentoFrameworkViewElementTemplateContext;
use MagentoSalesModelOrderFactory;
class Success extends MagentoCheckoutBlockSuccess {
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() {
$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

最新更新