我需要在收到付款后自动更改已完成的订单状态,但仅限于订单状态为“正在处理”.我找到了片段,在每种情况下使订单状态完成的原因,但是在成功付款更改后我的付款插件返回数据并更改“处理”的订单状态.我想在成功后将其更改为“已完成”,如果状态不是“处理”,则不要更改它.我遇到的主要问题是我不知道如何获得收到的状态订单.
这是我的代码:
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;
}
我已经弄清楚了.这是工作代码:
add_filter( 'woocommerce_thankyou',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;
}
更新:与WooCommerce版本3的兼容性
基于: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',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,true) == 'cod' || get_post_meta($order_id,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;
}
}