我需要在收到付款后自动将订单状态更改为已完成,但前提是订单状态为“正在处理”。我发现了这个片段,是什么让订单状态在每种情况下都完成,但是我的付款插件在成功付款更改后返回数据并更改“处理”的订单状态。我想在成功后将其更改为“已完成”,如果状态不是“正在处理”,则不要更改它。我遇到的主要问题是我不知道如何获取收到的状态订单。
这是我的代码:
add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 2 );
function update_order_status( $order_id ) {
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ('processing' == $order_status) {
$order->update_status( 'completed' );
}
//return $order_status;
}
Edit:
我已经想通了。这是适合我的代码:
add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 1 );
function update_order_status( $order_id ) {
if ( !$order_id ){
return;
}
$order = new WC_Order( $order_id );
if ( 'processing' == $order->status) {
$order->update_status( 'completed' );
}
return;
}
Update 2- 2019:使用WooCommerce:自动完成付款订单 https://stackoverflow.com/questions/35686707/woocommerce-auto-complete-paid-orders/35689563#35689563(更新的线程)
所以正确使用的钩子是woocommerce_payment_complete_order_status
过滤器返回完成
更新1: 与 WooCommerce 版本 3+ 的兼容性
我改变了答案
基于:WooCommerce - 自动完成付费虚拟订单(取决于付款方式) https://stackoverflow.com/questions/35686707/woocommerce-auto-complete-paid-virtual-orders-depending-on-payment-methods,您还可以处理条件中的所有付款方式:
// => not a filter (an action hook)
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
$order = new WC_Order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( get_post_meta($order_id, '_payment_method', true) == 'bacs' || get_post_meta($order_id, '_payment_method', true) == 'cod' || get_post_meta($order_id, '_payment_method', true) == 'cheque' ) {
return;
}
// "completed" updated status for paid "processing" Orders (with all others payment methods)
elseif ( $order->has_status( 'processing' ) ) {
$order->update_status( 'completed' );
}
else {
return;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)