douzhuanqian8244 2019-05-09 19:35 采纳率: 100%
浏览 260

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

Looking to create a custom order process that looks like this:

Customer adds item to cart During Checkout Process customer uploads design to order On Checkout payment is verified, but not captured After design is finalized by the company, they upload the proof for customer review the customer reviews proof on their dashboard, and clicks a button that processes the order and captures payment

I have figured out everything i need to make this happen except for how to allow the customer to change the status of their order in the dashboard. I do not need them to edit the order, just approve it for payment capture.

I think there should be an easy way to do this with custom PHP code in conjunction with a plugin like Woocommerce Status Control, but I can't seem to find a solution anywhere.

  • 写回答

1条回答 默认 最新

  • douque2016 2019-05-10 05:59
    关注

    You can use the following code that will:

    • Replace the button text "view" by "approve" on My account > Orders
    • Display a custom button to approve the order on My account > Order view (single order)
    • Display a custom success message once customer has approved an order

    This will only happen on customer orders with a specific status. So you will have to define:

    • The order status that require an approval from customer.
    • The order status that reflect an approved order by the customer (on the 3 functions)
    • The button text for order approval
    • The text that will be displayed once customer has approved the order

    The code:

    // 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
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.


    On My account > Orders (list):

    enter image description here

    On My account > Order view (when an order required to be approved):

    enter image description here

    On My account > Order view (when customer has approved the order):

    enter image description here


    For order statuses, you can create custom order statuses with code or with plugins.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c