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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持