4

注文状況、合計金額、支払い方法などの注文関連情報を取得しようとしています。

を使用して注文ステータスを取得しました

$_order->getStatusLabel();

しかし、Magento 2 で支払い方法関連の情報を取得する方法.

4

2 に答える 2

2

ブロックファイル内

/** @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;
}

phtmlファイルで

$_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";
}
于 2016-07-22T07:20:03.980 に答える