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

前端 未结 3 1698
慢半拍i
慢半拍i 2021-02-01 05:57

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\' ? ...          


        
相关标签:
3条回答
  • 2021-02-01 06:07

    2019 now, 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.

    Cheers.

    0 讨论(0)
  • 2021-02-01 06:09

    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();
    
    0 讨论(0)
  • 2021-02-01 06:13

    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 );
    
    0 讨论(0)
提交回复
热议问题