How to get all the information about the payment method of order in magento2

I am trying to get order information, such as order status, total amount, payment method, etc. I received order status using

$_order->getStatusLabel();

But how do I get information about the payment method in magento2.

+4
source share
1 answer

In a 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";
}
+2
source

Source: https://habr.com/ru/post/1647982/


All Articles