Magento 2 order get payment method
This time I show you how to get your orders payment method. Sometime this is needed if you want to do different processes for orders payed with different methods. It is also useful for accounting exports, sometimes other payments relates to other accounting records.
Magento 2 order get payment method
All you need to know for this is found in sales_order_payment table. There you will find detail information for your order payments. Column method lists all used payment methods. The method name is the full qualified method name, normally also with provider name. So they are distinguishable even if they have the same frontend name like “Creditcard”.
Get payment method programmatically
You only need a few lines of code to get all needed information.
1 2 3 4 5 6 | $payment = $order->getPayment(); $method = $payment->getMethodInstance(); $methodTitle = $method->getTitle(); echo "method: ".$payment->getMethod()."\n"; echo "title: ".$methodTitle."\n"; |
All you need is a fully loaded order object. See the difference between method and title on my example:
- method: mpay24cw_creditcard
- title: credit card
For a detailed distinction, you will use method in your if logic.
Conclusion
Separation of orders by payment method is often needed. Especially if you export orders to an external system or want to do accounting computation. A payment method always needs to have a unique name, so this separation is always possible.