WooCommerce Ajax获取订单状态 [英] WooCommerce ajax fetch order status

查看:84
本文介绍了WooCommerce Ajax获取订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WooCommerce商店将在几分钟内将订单状态更新为许多不同的状态.类似于在线比萨追踪器的工作方式,例如在烤箱中>烘烤>检查>交付.

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>

但是我想使用ajax和jQuery设置超时来每10秒更新一次状态.

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

setTimeout(fetchStatus, 10000);

我不确定要进行什么调用才能创建使用 global $ wooCommerce 变量的函数,并继续以这种方式进行更新?

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?

要确认这是在自定义woocommerce谢谢页面模板上.

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

推荐答案

这是一个解决方案.

HTML

更改此< h3 class ="order-status" id ="order_status"><?php echo $ order->状态;?></h3> .

AJAX脚本

<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

将其添加到主题的"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');

这篇关于WooCommerce Ajax获取订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆