允许客户在 WooCommerce 中更改订单状态 [英] Allow customer to change order status in WooCommerce
问题描述
在 WooCommerce 中,当订单处于处理状态时,我希望我的帐户"显示操作按钮的页面,允许客户通过将订单状态更改为完成来确认订单已到达.
我已经看到 允许客户更改通过电子邮件的订单相关问题代码(没有答案),这并没有真正帮助实现我的目标.
客户是否可以通过将订单状态更改为已完成"来确认订单是否已到达?
您可以使用 woocommerce_my_account_my_orders_actions
向我的帐户"添加自定义操作按钮订单部分,用于处理"订单状态(以及查看订单).
然后使用 template_redirect
钩子,客户可以更改其处理订单之一的状态,显示成功通知.
代码:
//按钮 Url 和标签函数 customer_order_confirm_args( $order_id ) {返回数组('网址' =>wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),'名称' =>__( '批准订单', 'woocommerce' ));}//添加自定义操作按钮来处理订单(我的账户 > 订单)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() );}返回 $actions;}//添加自定义按钮处理订单(我的账户>查看订单)add_action('woocommerce_order_details_after_order_table', 'complete_action_button_my_accout_order_view');函数 complete_action_button_my_accout_order_view( $order ){//避免在电子邮件通知上显示按钮如果(is_wc_endpoint_url('视图顺序')){$data = customer_order_confirm_args( $order->get_id() );echo '<div style="margin:16px 0 24px;"><a class="button";href='.$data['url'].'">'.$data['name'].'</a>
';}}//更改订单状态并显示消息add_action( 'template_redirect', 'action_complete_order_status' );函数 action_complete_order_status( $query ) {如果((is_wc_endpoint_url('订单')||is_wc_endpoint_url('视图顺序'))&&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']) );如果 ( is_a($order, 'WC_Order') ) {//将订单状态更改为已完成";$order->update_status('已完成', __('已由客户批准', 'woocommerce')) ;//添加通知(可选)wc_add_notice( sprintf( __( '订单#%s 已被批准', 'woocommerce' ), $order->get_id() );//删除查询参数wp_redirect(esc_url(remove_query_arg(array('complete_order','_wpnonce'))));出口();}}}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.
相关(旧答案):允许客户在 WooCommerce 我的帐户中更改订单状态
In WooCommerce, when an order is in processing status, I would like on "My Account" Page to display an action button allowing customer to confirm that the order has arrived by changing order status to complete.
I have seen Allowing customer to change status of the order via email related question code (without answers), that doesn't really help to achieve my goal.
Is it possible that the customer can confirm if the order has arrived by changing order status to "completed"?
You can use woocommerce_my_account_my_orders_actions
to add a custom action button to "My account" orders section, for orders with "processing" status (and also to view order).
Then using template_redirect
hook, it is possible for the customer to change the status of one of its processing orders, displaying a success notice.
The code:
// 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();
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related (old answer): Allow customer to change the order status in WooCommerce My account
这篇关于允许客户在 WooCommerce 中更改订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!