猿问

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

希望创建一个如下所示的自定义订单流程:


  • 客户将商品添加到购物车

  • 在结账过程中客户上传设计订单

  • 结账时付款已验证,但未捕获

  • 公司完成设计后,上传证明供客户审核

  • 客户在他们的仪表板上查看证明,然后单击一个按钮来处理订单并获取付款


  • 除了如何允许客户在仪表板中更改订单状态外,我已经想出了实现这一目标所需的一切。我不需要他们来编辑订单,只需批准它用于付款捕获。

    我认为应该有一种简单的方法来使用自定义 PHP 代码以及 Woocommerce Status Control 等插件来执行此操作,但我似乎无法在任何地方找到解决方案。


温温酱
浏览 245回答 2
2回答

慕侠2389804

新改进的答案:允许客户在 WooCommerce 中更改订单状态您可以使用以下代码:将“我的帐户”>“订单”中的“查看”按钮替换为“批准”在我的帐户 > 订单视图(单个订单)上显示一个自定义按钮以批准订单客户批准订单后显示自定义成功消息这只会发生在具有特定状态的客户订单上。所以你必须定义:需要客户批准的订单状态。反映客户批准订单的订单状态(在 3 个功能上)订单审批按钮文本客户批准订单后将显示的文本编码:// My account > Orders (list): Rename "view" action button text when order needs to be approvedadd_filter( 'woocommerce_my_account_my_orders_actions', 'change_my_account_my_orders_view_text_button', 10, 2 );function change_my_account_my_orders_view_text_button( $actions, $order ) {&nbsp; &nbsp; $required_order_status = 'processing'; // Order status that requires to be approved&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( $order->has_status($required_order_status) ) {&nbsp; &nbsp; &nbsp; &nbsp; $actions['view']['name'] = __("Approve", "woocommerce"); // Change button text&nbsp; &nbsp; }&nbsp; &nbsp; return $actions;}// My account > View Order: Add an approval button on the orderadd_action( 'woocommerce_order_details_after_order_table', 'approve_order_button_process' );function approve_order_button_process( $order ){&nbsp; &nbsp; // Avoiding displaying buttons on email notification&nbsp; &nbsp; if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;&nbsp; &nbsp; $approved_button_text&nbsp; = __("Approve this order", "woocommerce");&nbsp; &nbsp; $required_order_status = 'processing'; // Order status that requires to be approved&nbsp; &nbsp; $approved_order_status = 'completed'; // Approved order status&nbsp; &nbsp; // On submit change order status&nbsp; &nbsp; if( isset($_POST["approve_order"]) && $_POST["approve_order"] == $approved_button_text&nbsp; &nbsp; && $order->has_status( $required_order_status ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $order->update_status( $approved_order_status ); // Change order status&nbsp; &nbsp; }&nbsp; &nbsp; // Display a form with a button for order approval&nbsp; &nbsp; if( $order->has_status($required_order_status) ) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<form class="cart" method="post" enctype="multipart/form-data" style="margin-top:12px;">&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" class="button" name="approve_order" value="Approve this order" />&nbsp; &nbsp; &nbsp; &nbsp; </form>';&nbsp; &nbsp; }}// My account > View Order: Add a custom notice when order is approvedadd_action( 'woocommerce_order_details_before_order_table', 'approved_order_message' );function approved_order_message( $order ){&nbsp; &nbsp; // Avoiding displaying buttons on email notification&nbsp; &nbsp; if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;&nbsp; &nbsp; $approved_order_status = 'completed'; // Approved order status&nbsp; &nbsp; if( $order->has_status( $approved_order_status ) ) {&nbsp; &nbsp; &nbsp; &nbsp; wc_print_notice(&nbsp; __("This order is approved", "woocommerce"), 'success' ); // Message&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。在我的账户 > 订单(列表):在我的账户 > 订单视图(当需要批准订单时):在我的账户 > 订单视图(当客户批准订单时):对于订单状态,您可以使用代码或插件创建自定义订单状态。

子衿沉夜

我知道我可能会迟到。但在这里我回答这个问题,以防有人仍然需要它。下面的代码将在我的帐户订单页面操作列表中添加一个标记为已完成按钮。请注意,它会检查订单状态是否由管理员设置为已交付,然后向客户显示该按钮。我也为此开发了一个插件,它可以在 WordPress 存储库中使用。有需要的可以去看看。客户对 WooCommerce 的订单批准。/*================================&nbsp; &nbsp; Add Delivered Order Status==================================*/add_action( 'init', 'msoa_register_delivered_order_status' );function msoa_register_delivered_order_status() {&nbsp; &nbsp; register_post_status( 'wc-delivered', array(&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'Session Completed',&nbsp; &nbsp; &nbsp; &nbsp; 'public'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => true,&nbsp; &nbsp; &nbsp; &nbsp; 'show_in_admin_status_list' => true,&nbsp; &nbsp; &nbsp; &nbsp; 'show_in_admin_all_list'&nbsp; &nbsp; => true,&nbsp; &nbsp; &nbsp; &nbsp; 'exclude_from_search'&nbsp; &nbsp; &nbsp; &nbsp;=> false,&nbsp; &nbsp; &nbsp; &nbsp; 'label_count'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> _n_noop( 'Delivered <span class="count">(%s)</span>', 'Delivered <span class="count">(%s)</span>' )&nbsp; &nbsp; ) );}add_filter( 'wc_order_statuses', 'msoa_add_delivered_status_to_order_statuses' );function msoa_add_delivered_status_to_order_statuses( $order_statuses ) {&nbsp; &nbsp; $new_order_statuses = array();&nbsp; &nbsp; foreach ( $order_statuses as $key => $status ) {&nbsp; &nbsp; &nbsp; &nbsp; $new_order_statuses[ $key ] = $status;&nbsp; &nbsp; &nbsp; &nbsp; if ( 'wc-processing' === $key ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_order_statuses['wc-delivered'] = __( 'Delivered', 'order-approval' );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $new_order_statuses;}add_filter( 'woocommerce_my_account_my_orders_actions', 'msoa_mark_as_received', 10, 2 );function msoa_mark_as_received( $actions, $order ) {&nbsp; &nbsp; $order_id = $order->id;&nbsp; &nbsp; if ( ! is_object( $order ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $order_id = absint( $order );&nbsp; &nbsp; &nbsp; &nbsp; $order&nbsp; &nbsp; = wc_get_order( $order_id );&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // check if order status delivered and form not submitted&nbsp; &nbsp; if ( ( $order->has_status( 'delivered' ) ) && ( !isset( $_POST['mark_as_received'] ) ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $check_received = ( $order->has_status( 'delivered' ) ) ? "true" : "false";&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="ms-mark-as-received">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="mark_as_received" value="<?php echo esc_attr( $check_received ); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="order_id" value="<?php echo esc_attr($order_id);?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php wp_nonce_field( 'so_38792085_nonce_action', '_so_38792085_nonce_field' ); ?>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="int-button-small" type="submit" value="<?php echo esc_attr_e( 'Mark as Received', 'order-approval' ); ?>" data-toggle="tooltip" title="<?php echo esc_attr_e( 'Click to mark the order as complete if you have received the product', 'order-approval' ); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; //refresh page if form submitted&nbsp; &nbsp; * fix status not updating&nbsp; &nbsp; */&nbsp; &nbsp; if( isset( $_POST['mark_as_received'] ) ) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo "<meta http-equiv='refresh' content='0'>";&nbsp; &nbsp; }&nbsp; &nbsp; // not a "mark as received" form submission&nbsp; &nbsp; if ( ! isset( $_POST['mark_as_received'] ) ){&nbsp; &nbsp; &nbsp; &nbsp; return $actions;&nbsp; &nbsp; }&nbsp; &nbsp; // basic security check&nbsp; &nbsp; if ( ! isset( $_POST['_so_38792085_nonce_field'] ) || ! wp_verify_nonce( $_POST['_so_38792085_nonce_field'], 'so_38792085_nonce_action' ) ) {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $actions;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // make sure order id is submitted&nbsp; &nbsp; if ( ! isset( $_POST['order_id'] ) ){&nbsp; &nbsp; &nbsp; &nbsp; $order_id = intval( $_POST['order_id'] );&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( $order_id );&nbsp; &nbsp; &nbsp; &nbsp; $order->update_status( "completed" );&nbsp; &nbsp; &nbsp; &nbsp; return $actions;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; if ( isset( $_POST['mark_as_received'] ) == true ) {&nbsp; &nbsp; &nbsp; &nbsp; $order_id = intval( $_POST['order_id'] );&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( $order_id );&nbsp; &nbsp; &nbsp; &nbsp; $order->update_status( "completed" );&nbsp; &nbsp; }&nbsp; &nbsp; $actions = array(&nbsp; &nbsp; &nbsp; &nbsp; 'pay'&nbsp; &nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'url'&nbsp; => $order->get_checkout_payment_url(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => __( 'Pay', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'view'&nbsp; &nbsp;=> array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'url'&nbsp; => $order->get_view_order_url(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => __( 'View', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'cancel' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'url'&nbsp; => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => __( 'Cancel', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; );&nbsp; &nbsp; if ( ! $order->needs_payment() ) {&nbsp; &nbsp; &nbsp; &nbsp; unset( $actions['pay'] );&nbsp; &nbsp; }&nbsp; &nbsp; if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ), true ) ) {&nbsp; &nbsp; &nbsp; &nbsp; unset( $actions['cancel'] );&nbsp; &nbsp; }&nbsp; &nbsp; return $actions;}
随时随地看视频慕课网APP
我要回答