How to check payment method on a WooCommerce order by id?

belfort1 picture belfort1 · Sep 24, 2014 · Viewed 36.8k times · Source

I want to make some changes if the chosen payment method is COD. On existing WC_Order i have used

($order->payment_method_title == 'Cash On Delivery' ? ... : ... );

to retrieve the title. But i would like to check against the id (cod) because the title string gets translated to different languages which doesn't make it a good solution.

Is there a way to retrieve the id on a WC_Order in woocommerce?

Answer

helgatheviking picture helgatheviking · Sep 24, 2014

The post meta key for the payment method ID is simply _payment_method

So if $order->payment_method doesn't have the magic methods in place to get that automatically, you could retrieve the post meta using traditional WordPress

get_post_meta( $order->id, '_payment_method', true );

Update for WooCommerce 3.0

$order->get_payment_method();