【问题标题】:WooCommerce ajax fetch order statusWooCommerce ajax 获取订单状态
【发布时间】:2018-08-24 09:54:53
【问题描述】:

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

我目前正在感谢页面上显示我的订单状态 -

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

但我想使用 ajax 和可能的 jQuery 设置超时以每 10 秒更新一次状态。

setTimeout(fetchStatus, 10000);

我不确定我必须创建一个使用global $wooCommerce 变量的函数并继续以这种方式更新它?

确认这是在自定义 woocommerce 感谢页面模板上。

【问题讨论】:

  • 您是否可以在自定义感谢页面中获取订单 ID?
  • 是的,我可以访问普通的$order 变量以及它返回的内容,所以我可以做$order-&gt;get_order_number();

标签: php ajax wordpress woocommerce


【解决方案1】:

这里有一个解决方案。

HTML

更改此&lt;h3 class="order-status" id="order_status"&gt;&lt;?php echo $order-&gt;status; ?&gt;&lt;/h3&gt;

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”中。

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');

【讨论】:

  • 太棒了,非常感谢我从这个答案中学到了很多东西。谢谢
猜你喜欢
  • 2019-12-15
  • 2017-05-24
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2017-04-02
  • 2016-04-17
  • 1970-01-01
  • 2021-01-17
相关资源
最近更新 更多