亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

允許客戶在 WooCommerce 中更改訂單狀態

允許客戶在 WooCommerce 中更改訂單狀態

PHP
回首憶惘然 2023-08-19 17:52:53
在 WooCommerce 中,當訂單處于處理狀態時,我希望在“我的帳戶”頁面上顯示一個操作按鈕,允許客戶通過更改訂單狀態來確認訂單已到達以完成。我已經看到允許客戶通過電子郵件相關問題代碼更改訂單狀態(沒有答案),這并不能真正幫助實現我的目標。客戶是否可以通過將訂單狀態更改為“已完成”來確認訂單是否已到達?
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

您可以將woocommerce_my_account_my_orders_actions自定義操作按鈕添加到“我的帳戶”訂單部分,用于狀態為“正在處理”的訂單(也可以查看訂單)。


然后使用template_redirect鉤子,客戶可以更改其處理訂單之一的狀態,并顯示成功通知。


代碼:


// The button Url and the label

function customer_order_confirm_args( $order_id ) {

    return array(

        'url'  => wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),

        'name' => __( 'Approve order', '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( 'processing' ) ) {

        $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-order' ) ) {

        $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-order' ) )

        && 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 "completed"

            $order->update_status( 'completed', __('Approved by the customer', 'woocommerce') ) ;


            // Add a notice (optional)

            wc_add_notice( sprintf( __( 'Order #%s has been approved', 'woocommerce' ), $order->get_id() ) );


            // Remove query args

            wp_redirect( esc_url( remove_query_arg( array( 'complete_order', '_wpnonce' ) ) ) );

            exit();

        }

    }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。



查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 174 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號