Woocommerce更新帖子staus

时间:2015-09-09 09:59:46

标签: php wordpress woocommerce

我注册了很少的自定义帖子状态。而且我想在收到订单时挂钩。 我试试看:

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 ) );
}

但我失败了,帖子状态是wc处理 什么是正确的钩子?

1 个答案:

答案 0 :(得分:1)

使用WC_Order类的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 ); 
}