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

Php 允许客户更改我的帐户中的订单状态,php,wordpress,woocommerce,hook-woocommerce,orders,Php,Wordpress,Woocommerce,Hook Woocommerce,Orders,希望创建如下所示的自定义订单流程: 客户将商品添加到购物车 在结账过程中,客户将设计上传至订单 结账时付款已验证,但未捕获 公司完成设计后,他们上传证据供客户审查 客户查看其仪表板上的凭证,然后单击一个按钮来处理订单并捕获付款 我已经找到了实现这一点所需的一切,除了如何允许客户在仪表板中更改订单的状态。我不需要他们编辑订单,只需批准它进行付款捕获 我认为应该有一种简单的方法可以通过自定义PHP代码和Woocommerce状态控件这样的插件来实现这一点,但我似乎在任何地方都找不到解决方案 新的改进

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

  • 客户将商品添加到购物车
  • 在结账过程中,客户将设计上传至订单
  • 结账时付款已验证,但未捕获
  • 公司完成设计后,他们上传证据供客户审查
  • 客户查看其仪表板上的凭证,然后单击一个按钮来处理订单并捕获付款
  • 我已经找到了实现这一点所需的一切,除了如何允许客户在仪表板中更改订单的状态。我不需要他们编辑订单,只需批准它进行付款捕获

    我认为应该有一种简单的方法可以通过自定义PHP代码和Woocommerce状态控件这样的插件来实现这一点,但我似乎在任何地方都找不到解决方案

    新的改进答案:

    您可以使用以下代码:

    • 将我的帐户>订单上的按钮文本“查看”替换为“批准”
    • 显示自定义按钮以批准我的帐户上的订单>订单视图(单个订单)
    • 客户批准订单后显示自定义成功消息
    这只会发生在具有特定状态的客户订单上。因此,您必须定义:

    • 需要客户批准的订单状态
    • 反映客户批准订单的订单状态(在3个功能上)
    • 用于订单审批的按钮文本
    • 客户批准订单后将显示的文本
    守则:

    // My account > Orders (list): Rename "view" action button text when order needs to be approved
    add_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 ) {
        $required_order_status = 'processing'; // Order status that requires to be approved
        
        if( $order->has_status($required_order_status) ) {
            $actions['view']['name'] = __("Approve", "woocommerce"); // Change button text
        }
        return $actions;
    }
    
    // My account > View Order: Add an approval button on the order
    add_action( 'woocommerce_order_details_after_order_table', 'approve_order_button_process' );
    function approve_order_button_process( $order ){
        // Avoiding displaying buttons on email notification
        if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
    
        $approved_button_text  = __("Approve this order", "woocommerce");
        $required_order_status = 'processing'; // Order status that requires to be approved
        $approved_order_status = 'completed'; // Approved order status
    
        // On submit change order status
        if( isset($_POST["approve_order"]) && $_POST["approve_order"] == $approved_button_text
        && $order->has_status( $required_order_status ) ) {
            $order->update_status( $approved_order_status ); // Change order status
        }
    
        // Display a form with a button for order approval
        if( $order->has_status($required_order_status) ) {
            echo '<form class="cart" method="post" enctype="multipart/form-data" style="margin-top:12px;">
            <input type="submit" class="button" name="approve_order" value="Approve this order" />
            </form>';
        }
    }
    
    // My account > View Order: Add a custom notice when order is approved
    add_action( 'woocommerce_order_details_before_order_table', 'approved_order_message' );
    function approved_order_message( $order ){
        // Avoiding displaying buttons on email notification
        if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
    
        $approved_order_status = 'completed'; // Approved order status
    
        if( $order->has_status( $approved_order_status ) ) {
            wc_print_notice(  __("This order is approved", "woocommerce"), 'success' ); // Message
        }
    }
    
    //我的帐户>订单(列表):当订单需要批准时,重命名“查看”操作按钮文本
    添加过滤器('woocommerce\u my\u account\u my\u orders\u actions'、'change\u my\u account\u my\u orders\u view\u text\u button',10,2);
    功能更改\我的\帐户\我的\订单\查看\文本\按钮($actions,$order){
    $required_order_status=‘processing’;//需要批准的订单状态
    如果($order->has_status($required_order_status)){
    $actions['view']['name']=\uuu(“批准”、“woocommerce”);//更改按钮文本
    }
    返回$actions;
    }
    //我的帐户>查看订单:在订单上添加审批按钮
    添加操作('WOOMerce\u order\u details\u在\u order\u table之后,'approve\u order\u button\u process');
    功能批准\订单\按钮\流程($order){
    //避免在电子邮件通知上显示按钮
    if(!(is_wc_endpoint_url('view order')| is_wc_endpoint_url('order received'))返回;
    $approved_button_text=uuuuu(“批准此订单”,“woocommerce”);
    $required_order_status=‘processing’;//需要批准的订单状态
    $approved_order_status='completed';//已批准的订单状态
    //关于提交变更单状态
    如果(设置($\u POST[“批准订单”])和&$\u POST[“批准订单”]==$approved\u按钮文本
    &&$order->has_status($required_order_status)){
    $order->update_status($approved_order_status);//变更单状态
    }
    //显示带有订单审批按钮的表单
    如果($order->has_status($required_order_status)){
    回声'
    ';
    }
    }
    //我的帐户>查看订单:批准订单时添加自定义通知
    添加操作('WOOMerce\u order\u details\u before\u order\u table','approved\u order\u message');
    功能批准\订单\消息($order){
    //避免在电子邮件通知上显示按钮
    if(!(is_wc_endpoint_url('view order')| is_wc_endpoint_url('order received'))返回;
    $approved_order_status='completed';//已批准的订单状态
    如果($order->has_status($approved_order_status)){
    wc_print_通知(uuu(“此订单已批准”,“woocommerce”),“成功”);//消息
    }
    }
    
    代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


    我的账户>订单(列表):

    在我的帐户上>订单视图(当订单需要批准时):

    在我的帐户上>订单视图(当客户批准订单时):



    对于订单状态,您可以创建自定义订单状态或使用插件。

    我知道我可能有点晚了。但我在这里回答这个问题,以防有人还需要它

    下面的代码将在“我的帐户订单”页面操作列表中添加“标记为已完成”按钮。请注意,它检查订单状态是否设置为由管理员交付,然后向客户显示按钮

    我也为此开发了一个插件,它可以在WordPress存储库中找到。如果你需要的话,你可以去看看

    /*================================
    添加已交货订单状态
    ==================================*/
    添加操作('init'、'msoa'寄存器\交付\订单\状态');
    功能msoa\u寄存器\u已交付\u订单\u状态(){
    注册后状态(“wc已交付”,数组(
    “标签”=>“会话已完成”,
    “public”=>正确,
    “在管理状态列表中显示”=>true,
    “在\u管理\u所有\u列表中显示\u”=>true,
    “从搜索中排除”=>false,
    “标签计数”=>\n\u noop('Delivered(%s)'Delivered(%s)'
    ) );
    }
    添加过滤器(“wc\U订单状态”、“msoa\U将交付状态添加到订单状态”);
    功能msoa_将_已交付_状态_添加到_订单_状态($order_状态){
    $new_order_status=array();
    foreach($key=>$status的订单状态){
    $new_order_status[$key]=$status;
    if('wc processing'==$key){
    $new_order_status['wc-delivered']==uu('delivered','order approval');
    }
    }
    返回$new\u order\u状态;
    }
    添加过滤器('woocommerce\u my\u account\u my\u orders\u actions','msoa\u mark\u as\u received',10,2);
    功能msoa_将_标记为已接收($actions,$order)
    
    /*================================
        Add Delivered Order Status
    ==================================*/
    
    add_action( 'init', 'msoa_register_delivered_order_status' );
    function msoa_register_delivered_order_status() {
        register_post_status( 'wc-delivered', array(
            'label'                     => 'Session Completed',
            'public'                    => true,
            'show_in_admin_status_list' => true,
            'show_in_admin_all_list'    => true,
            'exclude_from_search'       => false,
            'label_count'               => _n_noop( 'Delivered <span class="count">(%s)</span>', 'Delivered <span class="count">(%s)</span>' )
        ) );
    }
    
    add_filter( 'wc_order_statuses', 'msoa_add_delivered_status_to_order_statuses' );
    function msoa_add_delivered_status_to_order_statuses( $order_statuses ) {
    
        $new_order_statuses = array();
    
        foreach ( $order_statuses as $key => $status ) {
    
            $new_order_statuses[ $key ] = $status;
    
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-delivered'] = __( 'Delivered', 'order-approval' );
            }
        }
    
        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 ) {
        $order_id = $order->id;
    
        if ( ! is_object( $order ) ) {
            $order_id = absint( $order );
            $order    = wc_get_order( $order_id );
        }
        
        // check if order status delivered and form not submitted
    
        if ( ( $order->has_status( 'delivered' ) ) && ( !isset( $_POST['mark_as_received'] ) ) ) {
            $check_received = ( $order->has_status( 'delivered' ) ) ? "true" : "false";
            ?>
            <div class="ms-mark-as-received">
                <form method="post">
                    <input type="hidden" name="mark_as_received" value="<?php echo esc_attr( $check_received ); ?>">
                    <input type="hidden" name="order_id" value="<?php echo esc_attr($order_id);?>">
                    <?php wp_nonce_field( 'so_38792085_nonce_action', '_so_38792085_nonce_field' ); ?> 
                    <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' ); ?>">
                </form>
            </div>
            <?php
        }
    
        /*
        //refresh page if form submitted
    
        * fix status not updating
        */
        if( isset( $_POST['mark_as_received'] ) ) { 
            echo "<meta http-equiv='refresh' content='0'>";
        }
    
        // not a "mark as received" form submission
        if ( ! isset( $_POST['mark_as_received'] ) ){
            return $actions;
        }
    
        // basic security check
        if ( ! isset( $_POST['_so_38792085_nonce_field'] ) || ! wp_verify_nonce( $_POST['_so_38792085_nonce_field'], 'so_38792085_nonce_action' ) ) {   
            return $actions;
        } 
    
        // make sure order id is submitted
        if ( ! isset( $_POST['order_id'] ) ){
            $order_id = intval( $_POST['order_id'] );
            $order = wc_get_order( $order_id );
            $order->update_status( "completed" );
            return $actions;
        }  
        if ( isset( $_POST['mark_as_received'] ) == true ) {
            $order_id = intval( $_POST['order_id'] );
            $order = wc_get_order( $order_id );
            $order->update_status( "completed" );
        }
    
        $actions = array(
            'pay'    => array(
                'url'  => $order->get_checkout_payment_url(),
                'name' => __( 'Pay', 'woocommerce' ),
            ),
            'view'   => array(
                'url'  => $order->get_view_order_url(),
                'name' => __( 'View', 'woocommerce' ),
            ),
            'cancel' => array(
                'url'  => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
                'name' => __( 'Cancel', 'woocommerce' ),
            ),
        );
    
        if ( ! $order->needs_payment() ) {
            unset( $actions['pay'] );
        }
    
        if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ), true ) ) {
            unset( $actions['cancel'] );
        }
    
        return $actions;
    
    }