如何在magento2中获取所有订单支付方式信息

How to get all order payment method information in magento2

我尝试获取订单相关信息,如订单状态、总金额、付款方式等

我使用

获得了订单状态
$_order->getStatusLabel();

但是我如何在 Magento 2 中获取与付款方式相关的信息。

In Block file

/** @var \Magento\Sales\Model\ResourceModel\Order\Payment\Collection    */
protected $_paymentCollectionFactory;
 /** @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory */
protected $_orderCollectionFactory;
/** @var \Magento\Sales\Model\ResourceModel\Order\Collection */
protected $orders;

 public function __construct(
\Magento\Framework\View\Element\Template\Context $context,\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\ResourceModel\Order\Payment\CollectionFactory $paymentCollectionFactory, array $data = []
) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_paymentCollectionFactory = $paymentCollectionFactory;
    parent::__construct($context, $data);
}
 public function getPaymentsCollection() {
    $collection = $this->_paymentCollectionFactory->create()->addFieldToSelect('*');
    return $collection;
}
public function getOrders($storeId) {
if (!$this->orders) {
            $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*');
        }
 return $this->orders;
}

In phtml file

$_orders = $block->getOrders();

if ($_orders && count($_orders)) {

$payments = $block->getPaymentsCollection();
$chekmo = $cod = $free = $bank = $paypalgrp = 0;
foreach ($payments as $_payment) {
    $method = $_payment->getMethod();
    switch ($method) {
        case 'checkmo' : $chekmo++;
            break;
        case 'cashondelivery' : $cod++;
            break;
        case 'free' : $free++;
            break;
        case 'banktransfer' : $bank++;
            break;
        case 'paypal_group_all_in_one' : $paypalgrp++;
            break;
    }
  }

    echo "Payment Methods<br>";
    echo "Check / Money Order Methods " . $ckeckmo;
    echo "Cash on Delivery Methods " . $cod;
    echo "Free Methods " . $free;
    echo "Bank Transfer Methods " . $bank;
    echo "Paypal group all in one Methods " . $paypalgrp;
}
else{
 echo "You have no Orders";
}

以下代码会有所帮助

      $orderId = 1001;
      $order = $this->order->load($orderId);


      $method=$order->getPayment()->getMethod(); 
      $expmonth=$order->getPayment()->getCcExpMonth();
      $expyear=$order->getPayment()->getCcExpYear();
      $cclast4=$order->getPayment()->getCcLast4();
      $ipaddress=$order->getRemoteIp();

      $billingaddress=$order->getBillingAddress();
      $billingcity=$billingaddress->getCity();      
      $billingstreet=$billingaddress->getStreet();
      $billingpostcode=$billingaddress->getPostcode();
      $billingtelephone=$billingaddress->getTelephone();
      $billingstate_code=$billingaddress->getRegionCode();

      $shippingaddress=$order->getShippingAddress();        
      $shippingcity=$shippingaddress->getCity();
      $shippingstreet=$shippingaddress->getStreet();
      $shippingpostcode=$shippingaddress->getPostcode();      
      $shippingtelephone=$shippingaddress->getTelephone();
      $shippingstate_code=$shippingaddress->getRegionCode();


      $tax_amount=$order->getTaxAmount();
      $total=$order->getGrandTotal();

工作起来很有魅力