Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 536
belfort1 Avatar asked Sep 24 '14 08:09

belfort1


People also ask

How do I view my payment method in WooCommerce?

Go to: WooCommerce > Settings > Payments. Use the toggle under Enabled to select Check Payments. Select Set Up. You are taken to the Check Payments settings.

How do I find my transaction ID in WooCommerce?

On the right side of the screen, you can view logs from whatever time the requests were made. Within those logs if you scroll down, you will see a SHQ_REQUEST_20xx_xxxx_xxx_xxxxxxxx. This is the Transaction ID.


Video Answer


3 Answers

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();
like image 178
helgatheviking Avatar answered Oct 19 '22 19:10

helgatheviking


If you want the title of the payment method you can do:

$order = new WC_Order( $order_id );
$payment_title = $order->get_payment_method_title();

This returns the string set in the Woocommerce > Payment methods, ex: Paypal.

Here are some very helpful references and documentation that will help you do anything you want with WooCommerce.

  • WooCommerce Order Class
  • WooCommerce Reference Docs

Cheers.

like image 7
Caio Mars Avatar answered Oct 19 '22 21:10

Caio Mars


If you need the payment gateway object itself you can use the wc_get_payment_gateway_by_order function.

$payment_gateway = wc_get_payment_gateway_by_order( $order );
like image 6
forsvunnet Avatar answered Oct 19 '22 20:10

forsvunnet