wordpress 如何通过 ID 检查 WooCommerce 订单的付款方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26011837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check payment method on a WooCommerce order by id?
提问by belfort1
I want to make some changes if the chosen payment method is COD. On existing WC_Order i have used
如果选择的付款方式是 COD,我想进行一些更改。在我使用过的现有 WC_Order 上
($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.
检索标题。但我想检查 id (cod),因为标题字符串被翻译成不同的语言,这并不是一个好的解决方案。
Is there a way to retrieve the id on a WC_Order in woocommerce?
有没有办法在woocommerce中检索WC_Order上的id?
回答by helgatheviking
The post meta key for the payment method ID is simply _payment_method
付款方式 ID 的 post 元键很简单 _payment_method
So if $order->payment_methoddoesn't have the magic methods in place to get that automatically, you could retrieve the post meta using traditional WordPress
因此,如果$order->payment_method没有自动获取的神奇方法,您可以使用传统的 WordPress 检索帖子元数据
get_post_meta( $order->id, '_payment_method', true );
Update for WooCommerce 3.0
WooCommerce 3.0 更新
$order->get_payment_method();
回答by forsvunnet
If you need the payment gateway object itself you can use the wc_get_payment_gateway_by_orderfunction.
如果您需要支付网关对象本身,您可以使用该wc_get_payment_gateway_by_order功能。
$payment_gateway = wc_get_payment_gateway_by_order( $order );
回答by ColdTuna
2019 now, if you want the title of the payment method you can do:
2019 现在,如果您想要付款方式的标题,您可以:
$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.
这将返回 Woocommerce > Payment methods 中设置的字符串,例如:Paypal。
Cheers.
干杯。

