猿问

允许客户在 WooCommerce 中更改订单状态

在 WooCommerce 中,当订单处于处理状态时,我希望在“我的帐户”页面上显示一个操作按钮,允许客户通过更改订单状态来确认订单已到达以完成。

我已经看到允许客户通过电子邮件相关问题代码更改订单状态(没有答案),这并不能真正帮助实现我的目标。

客户是否可以通过将订单状态更改为“已完成”来确认订单是否已到达?


回首忆惘然
浏览 117回答 1
1回答

qq_笑_17

您可以将woocommerce_my_account_my_orders_actions自定义操作按钮添加到“我的帐户”订单部分,用于状态为“正在处理”的订单(也可以查看订单)。然后使用template_redirect钩子,客户可以更改其处理订单之一的状态,并显示成功通知。代码:// The button Url and the labelfunction customer_order_confirm_args( $order_id ) {&nbsp; &nbsp; return array(&nbsp; &nbsp; &nbsp; &nbsp; 'url'&nbsp; => wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),&nbsp; &nbsp; &nbsp; &nbsp; 'name' => __( 'Approve order', 'woocommerce' )&nbsp; &nbsp; );}// 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 ) {&nbsp; &nbsp; if ( $order->has_status( 'processing' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $actions['order_confirmed'] = customer_order_confirm_args( $order->get_id() );&nbsp; &nbsp; }&nbsp; &nbsp; 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 ){&nbsp; &nbsp; // Avoiding displaying buttons on email notification&nbsp; &nbsp; if( is_wc_endpoint_url( 'view-order' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $data = customer_order_confirm_args( $order->get_id() );&nbsp; &nbsp; &nbsp; &nbsp; echo '<div style="margin:16px 0 24px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="button" href="'.$data['url'].'">'.$data['name'].'</a>&nbsp; &nbsp; &nbsp; &nbsp; </div>';&nbsp; &nbsp; }}// Change order status and display a messageadd_action( 'template_redirect', 'action_complete_order_status' );function action_complete_order_status( $query ) {&nbsp; &nbsp; if ( ( is_wc_endpoint_url( 'orders' )&nbsp; &nbsp; &nbsp; &nbsp; || is_wc_endpoint_url( 'view-order' ) )&nbsp; &nbsp; &nbsp; &nbsp; && isset( $_GET['complete_order'] )&nbsp; &nbsp; &nbsp; &nbsp; && $_GET['complete_order'] > 1&nbsp; &nbsp; &nbsp; &nbsp; && isset($_GET['_wpnonce'])&nbsp; &nbsp; &nbsp; &nbsp; && wp_verify_nonce($_GET['_wpnonce'], 'wc_complete_order') )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( absint($_GET['complete_order']) );&nbsp; &nbsp; &nbsp; &nbsp; if ( is_a($order, 'WC_Order') ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Change order status to "completed"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $order->update_status( 'completed', __('Approved by the customer', 'woocommerce') ) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add a notice (optional)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf( __( 'Order #%s has been approved', 'woocommerce' ), $order->get_id() ) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove query args&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_redirect( esc_url( remove_query_arg( array( 'complete_order', '_wpnonce' ) ) ) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
随时随地看视频慕课网APP
我要回答