0

I'm unable to get the payment method from the sales_order_save_after observer class in a custom module, i can get the orderId and Name but i have been unable to work out how to get the payment method (paypal, bank transfer etc)

My observer class:

class MyObserver implements ObserverInterface { 

  public function execute(\Magento\Framework\Event\Observer $observer) {

      $order = $observer->getEvent()->getOrder();
      $orderId = $order->getId();
      $customerName = "";

      //$payment = $observer->getEvent()->getPayment();
      //$paymentmethod = $payment->getMethod();

      if ($order->getCustomerFirstname()) {
        $customerName = $order->getCustomerName();
      } else {
       // guest customer
        $billingAddress = $order->getBillingAddress();
        $customerName = $billingAddress->getFirstname() . ' ' . 
        $billingAddress->getLastname();
      }

      $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/debug.log');
      $logger = new \Zend\Log\Logger();

      $logger->addWriter($writer);
      $logger->info('User has placed a new order. Order ID: ' . $orderId . '. Name: ' . $customerName . ". Payment Method: " . $paymentMethod);
 }

}

The $paymentmethod commented out throws an error :

 "Fatal Error: 'Uncaught Error: Call to a member function getMethod()
 on null in
1
  • $paymentmethod = $order->getPayment()->getMethod()
    – PЯINCƎ
    Jan 16, 2020 at 9:20

1 Answer 1

1

It should work with

$payment = $order->getPayment();
$method = $payment->getMethod();
1
  • Fatal Error: 'Uncaught Error: Call to a member function getMethod() on null in
    – gazZz
    Jan 16, 2020 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.