Change default order status for COD payment gateway based on user role in Woocommerce

微笑、不失礼 提交于 2019-12-11 08:38:58

问题


In Woocommerce, when the payment option is COD, the orders go directly to the "Processing" state.

Source: https://docs.woocommerce.com/document/managing-orders/#prettyPhoto

I need this to work like this, except when the client's role is "X".

I have seen that this can be solved with this code:

function cod_payment_method_order_status_to_onhold( $order_id ) {
    if ( ! $order_id )
        return;

    $order = wc_get_order( $order_id );

    if (  get_post_meta($order->id, '_payment_method', true) == 'cod' )
        $order->update_status( 'on-hold' );
}

add_action( 'woocommerce_thankyou', 'cod_payment_method_order_status_to_onhold', 10, 1 );

However, the problem is that it goes through "Processing", sends the email and then goes to "on hold". I want to avoid sending the "Processing" mail

Any way to do it? Thank you!


回答1:


You should better use woocommerce_cod_process_payment_order_status dedicated filter hook. You will have to replace "administrator" user role by your "X" role.

The hooked function code:

add_filter( 'woocommerce_cod_process_payment_order_status', 'set_cod_process_payment_order_status_on_hold', 10, 2 );
function set_cod_process_payment_order_status_on_hold( $status, $order ) {
    $user_data = get_userdata( $order->get_customer_id() );
    if( ! in_array( 'administrator', $user_data->roles ) )
        return 'on-hold';
    return $status;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/50824510/change-default-order-status-for-cod-payment-gateway-based-on-user-role-in-woocom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!