在 Woocommerce 3 中保留并获取带有时间戳的订单状态历史记录 [英] Keep and get order status history with their time stamps in Woocommerce 3

查看:118
本文介绍了在 Woocommerce 3 中保留并获取带有时间戳的订单状态历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存 woocommerce 上所有订单的订单状态历史记录.例如,如果订单状态从待处理更改为处理之前的状态及其时间戳应保存,以便用户可以通过此

I want to save the order status history for all orders on woocommerce. e.g If the order status is changed from pending to processing the previous status along with its timestamp should be saved so that user can view their order status history through this page. I have done some research but I couldn't find a way to do this.

我想听听建议.订单跟踪是使用自定义插件完成的.我正在使用以下 $order 对象检索订单详细信息

I would like to hear the suggestions. The order tracking is done using a custom plugin. I am retrieving the order  details using the following $order object

我正在使用此对象检索帐单、运输详细信息、状态和产品详细信息.使用此对象维护和获取订单状态历史记录对我来说会更容易一些.以下是我通过上述方法得到的对象数据

$order = wc_get_order( $order_id );

I am retrieving the billing, shipping details, status and products details using this object. It would be somewhat easier for me to maintain and get the order status history using this object. Following is the object data I get from the above method

推荐答案

您可以使用自定义字段来保留订单状态历史记录.然后您将使用以下挂钩函数:

解决方案
  • 在客户下订单后开始在订单创建时注册待处理状态,
  • 每次状态更改时更新历史记录.

代码:

The code:

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

// Initiating the history process registering pending status on order creation add_action( 'woocommerce_checkout_create_order', 'init_order_status_history', 20, 4 ); function init_order_status_history( $order, $data ){ // Set the default time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/Paris'); // Init order status history on order creation. $order->update_meta_data( '_status_history', array( time() => 'pending' ) ); } // Getting each status change history and saving the data add_action( 'woocommerce_order_status_changed', 'order_status_history', 20, 4 ); function order_status_history( $order_id, $old_status, $new_status, $order ){ // Set the default time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/Paris'); // Get order status history $order_status_history = $order->get_meta( '_status_history' ) ? $order->get_meta( '_status_history' ) : array(); // Add the current timestamp with the new order status to the history array $order_status_history[time()] = $new_status; // Update the order status history (as order meta data) $order->update_meta_data( '_status_history', $order_status_history ); $order->save(); // Save }

以下短代码将输出不同的订单状态和时间戳:

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

The following shortcode will output the different order statuses and timestamps:
';//遍历状态(和时间戳)foreach( $status_history as $timestamp => $order_status ){输出 .= '<div class="status-step">'.ucfirst( $order_status ) .'
<span class="date-time">'.date('Y-m-d H:i:s', $timestamp ) .'</span></div>';}返回 $output .'</div>';}

add_shortcode( 'status_history', 'get_order_status_history' ); function get_order_status_history(){ // Get an instance of the WC_Order object from the order ID $order = wc_get_order( $order_id ); // Get the history data $status_history = $order->get_meta('_status_history'); $output = '<div class="order-statuses-container">'; // Loop through the statuses (and timestamps) foreach( $status_history as $timestamp => $order_status ){ output .= '<div class="status-step">' . ucfirst( $order_status ) . '<br> <span class="date-time">' . date('Y-m-d H:i:s', $timestamp ) . '</span></div>'; } return $output . '</div>'; }

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

您仍然需要:

  • 设置正确的日期格式(使用date() 文档)
  • 在两个第一个函数中设置正确的时区
  • 在短代码函数中设置正确的html结构输出.
  • 根据需要添加一些 CSS 规则到主题的 style.css 文件中

用法:

1) 在文章或页面的 Wordpress 文本编辑器中:[status_history]

1) In the Wordpress text editor of a post or a page: [status_history]

2) 在 PHP 代码中:echo do_shortcode( "[status_history]" );

2) In PHP code: echo do_shortcode( "[status_history]" );

这篇关于在 Woocommerce 3 中保留并获取带有时间戳的订单状态历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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