dqd2800 2018-08-24 09:54
浏览 74
已采纳

WooCommerce ajax获取订单状态

My WooCommerce store will be updating the order status within a few minutes to a number of different states. Similar to how online pizza trackers work e.g. in the oven > baking > checking > delivering.

I'm currently displaying my order status on the thank you page -

<h3 class="order-status"><?php echo $order->status; ?></h3>

But I want to use ajax and possibly a jQuery set time out to update the status every 10 seconds.

setTimeout(fetchStatus, 10000);

I'm not sure what call to make will I have to create a function that uses the global $wooCommerce variable and keep updating it that way?

To confirm this is on a custom woocommerce thankyou page template.

  • 写回答

1条回答 默认 最新

  • dsbfbz75185 2018-08-24 10:55
    关注

    Here is a solution.

    HTML

    Change this <h3 class="order-status" id="order_status"><?php echo $order->status; ?></h3>.

    AJAX Script

    <script>
    function fetchStatus()
    {
        jQuery.ajax({
            url : '<?php echo site_url(); ?>/wp-admin/admin-ajax.php?action=fetch_order_status&order_id=<?php echo $order->get_order_number(); ?>',
            type : 'post',      
            error : function(response){
                console.log(response);
            },
            success : function( response ){
                jQuery('#order_status').text(response);
            }
        });
    }
    
    setInterval(fetchStatus, 1000);
    </script>
    

    functions.php

    Add this to your theme's 'functions.php'.

    function fetch_order_status(){
        $order = wc_get_order( $_REQUEST['order_id'] );
        $order_data = $order->get_data();
        echo $order_data['status'];
        die();
    }
    
    add_action('wp_ajax_nopriv_fetch_order_status', 'fetch_order_status');
    add_action('wp_ajax_fetch_order_status','fetch_order_status');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器