Woocommerce 更新 post 个状态

Woocommerce update post staus

我注册了几个自定义 post 状态。我想在收到订单时挂钩。 我试了一下:

add_action('woocommerce_checkout_order_processed', 'aa_func_20151609121636',30, 2);
function aa_func_20151609121636($order_id, $posted)
{
    global $wpdb;
    $post_status = null;

if(isset($_POST['yes_its_enq']) && ($_POST['yes_its_enq'] === 'yes')) {
    $post_status = 'wc-gibraenquiry';
} else {
    $post_status ='wc-gibrapending';
}

$wpdb->update( $wpdb->posts, array( 'post_status' => $post_status ), array( 'ID' => $order_id ) );
}

但我失败了,post 状态是 wc-processing 什么钩子合适呢?

使用WC_Orderclass的update_status方法更新状态。

试试下面的代码:

add_action('woocommerce_checkout_order_processed', 'aa_func_20151609121636',30, 2);

function aa_func_20151609121636($order_id, $posted)
{

    $order = new WC_Order( $order_id );

    if( isset( $posted['yes_its_enq'] ) && ( $posted['yes_its_enq'] === 'yes' ) ) {

        $post_status = 'wc-gibraenquiry';

    } else {

        $post_status ='wc-gibrapending';

    }

    $order->update_status( $post_status ); 
}