Magento – run observer function depend on payment method, how

dependencyevent-observerpayment-methodsshipping-methods

I want to run an observer function depend on payment method, how can I do this?

public function CompleteOrder($observer)
{
    ......
}

Now I want to run this function only when payment method is cashondelivery ….!

when I use code below, in the frontend, when customer click on final button for checkout, if payment method be not cashondelivery then order can not be completed!

public function CompleteOrder($observer)
{
    $method = $observer->getEvent()->getMethodInstance();
    if ($method->getCode() == 'cashondelivery') 
    {
       ...
    } else {
       return false;
}

Best Answer

I am not sure what event you are listening to but what I would suggest would be using the event sales_model_service_quote_submit_success this should be triggered after any quote is successfully transformed into an order.

One of the items passed to the event is the order object itself and to get the payment method you can do as follows.

$order = $observer->getEvent()->getOrder();
$payment_method_code = $order->getPayment()->getMethodInstance()->getCode();