![](/img/trans.png)
[英]Show Magento custom (yes/no) Attribute value in success.phtml
[英]Magento 2 Success.phtml Show Payment Method
我需要在Magento 2网站的success.phtml页面中显示付款方式代码或名称。
我创建模块; 然后在success.phtml中添加“您的付款方式是:”一句话; 但是,无法在此处编写付款方式。
我只想说以下话:
<?= __('Your order method is:',$block->getPaymentMethod()) ?>
您能举例说明我的模块函数php创建paymentmethod()函数吗?
我尝试了stackoverflow中的所有内容
这些链接没有帮助我:
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;
}
}
尝试这个:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.