如何更改状态按钮

时间:2021-07-03 20:07:14

标签: php woocommerce

我正在尝试制作一个按钮来更改我的订单状态,但它只能在订单状态为:ywraq-pending 时显示在查看报价页面上

我正在尝试此代码,但它显示在所有状态中:

function customer_order_confirm_args( $order_id ) {
    return array(
        'url'  => wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),
        'name' => __( 'Aprovar Orçamento', 'woocommerce' )
    );
}

// Add a custom action button to processing orders (My account > Orders)
add_filter( 'woocommerce_my_account_my_orders_actions', 'complete_action_button_my_accout_orders', 50, 2 );
function complete_action_button_my_accout_orders( $actions, $order ) {
    if ( $order->has_status( 'ywraq-pending' ) ) {
        $actions['order_confirmed'] = customer_order_confirm_args( $order->get_id() );
    }
    return $actions;
}

// Add a custom button to processing orders (My account > View order)
add_action( 'woocommerce_order_details_after_order_table', 'complete_action_button_my_accout_order_view' );
function complete_action_button_my_accout_order_view( $order ){
    // Avoiding displaying buttons on email notification
    if( is_wc_endpoint_url( 'view-quote' ) ) {
        $data = customer_order_confirm_args( $order->get_id() );

        echo '<div style="margin:16px 0 24px;">
            <a class="button" href="'.$data['url'].'">'.$data['name'].'</a>
        </div>';
    }
}

// Change order status and display a message
add_action( 'template_redirect', 'action_complete_order_status' );
function action_complete_order_status( $query ) {
    if ( ( is_wc_endpoint_url( 'orders' )
        || is_wc_endpoint_url( 'view-quote' ) )
        && isset( $_GET['complete_order'] )
        && $_GET['complete_order'] > 1
        && isset($_GET['_wpnonce'])
        && wp_verify_nonce($_GET['_wpnonce'], 'wc_complete_order') )
    {
        $order = wc_get_order( absint($_GET['complete_order']) );

        if ( is_a($order, 'WC_Order') ) {
            // Change order status to "ywraq-accepted"
            $order->update_status( 'ywraq-accepted', __('Approvado pelo cliente', 'woocommerce') ) ;

            // Add a notice (optional)
            wc_add_notice( sprintf( __( 'Pedido #%s foi aprovado', 'woocommerce' ), $order->get_id() ) );

            // Remove query args
            wp_redirect( esc_url( remove_query_arg( array( 'complete_order', '_wpnonce' ) ) ) );
            exit();
        }
    }
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

如果我清楚地理解您的问题,您错过了在 complete_action_button_my_accout_order_view 函数中检查订单状态,因此,您可以使用以下代码实现:

function customer_order_confirm_args( $order_id ) {
    return array(
        'url'  => wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),
        'name' => __( 'Aprovar Orçamento', 'woocommerce' )
    );
}

// Add a custom action button to processing orders (My account > Orders)
add_filter( 'woocommerce_my_account_my_orders_actions', 'complete_action_button_my_accout_orders', 50, 2 );
function complete_action_button_my_accout_orders( $actions, $order ) {
    if ( $order->has_status( 'ywraq-pending' ) ) {
        $actions['order_confirmed'] = customer_order_confirm_args( $order->get_id() );
    }
    return $actions;
}

// Add a custom button to processing orders (My account > View order)
add_action( 'woocommerce_order_details_after_order_table', 'complete_action_button_my_accout_order_view' );
function complete_action_button_my_accout_order_view( $order ){
    // Avoiding displaying buttons on email notification && only for orders with ywraq-pending status
    if( is_wc_endpoint_url( 'view-quote' ) 
        && $order->has_status( 'ywraq-pending' ) ) {

        $data = customer_order_confirm_args( $order->get_id() );

        echo '<div style="margin:16px 0 24px;"><a class="button" href="'.$data['url'].'">'.$data['name'].'</a></div>';
    }
}

// Change order status and display a message
add_action( 'template_redirect', 'action_complete_order_status' );
function action_complete_order_status( $query ) {
    if ( ( is_wc_endpoint_url( 'orders' )
        || is_wc_endpoint_url( 'view-quote' ) )
        && isset( $_GET['complete_order'] )
        && $_GET['complete_order'] > 1
        && isset($_GET['_wpnonce'])
        && wp_verify_nonce($_GET['_wpnonce'], 'wc_complete_order') )
    {
        $order = wc_get_order( absint($_GET['complete_order']) );

        if ( is_a($order, 'WC_Order') ) {
            // Change order status to "ywraq-accepted"
            $order->update_status( 'ywraq-accepted', __('Approvado pelo cliente', 'woocommerce') ) ;

            // Add a notice (optional)
            wc_add_notice( sprintf( __( 'Pedido #%s foi aprovado', 'woocommerce' ), $order->get_id() ) );

            // Remove query args
            wp_redirect( esc_url( remove_query_arg( array( 'complete_order', '_wpnonce' ) ) ) );
            exit();
        }
    }
}