使用
\Magento\Sales\Api\OrderRepositoryInterface
示例:
<?php
namespace Magento\Sfshipping\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
parent::__construct($context);
}
/**
* @johnny wei
*/
public function getPaymentData()
{
$orderId = 1;
$order = $this->orderRepository->get($orderId);
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
echo $method->getTitle(); // Cash On Delivery
echo $method->getCode(); // cashondelivery
}
}
付费内容限时免费中...
2.使用
Magento\Sales\Model\Order
示例
<?php
namespace Magento\Sfshipping\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Sales\Model\Order $order
) {
$this->_order=$order;
parent::__construct($context);
}
/**
* @johnny wei
*/
public function getPaymentData()
{
$order=$this->_order->loadByIncrementId('000000021');
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
echo $method->getTitle(); // Cash On Delivery
echo $method->getCode();
}一般习惯是我们写插件,大多数使用第一种,只加载必要项,性能有优势,如果您需要读取order中其它内容,那么用方法二是不错的选择,按需求处理




