Keep and get order status history with their time stamps in Woocommerce 3

拥有回忆 提交于 2021-02-07 10:24:09

问题


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.

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

object(WC_Order)#3738 (14) { ["status_transition":protected]=> bool(false) ["data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(10) "processing" ["currency"]=> string(3) "GBP" ["version"]=> string(5) "3.5.0" ["prices_include_tax"]=> bool(false) ["date_created"]=> object(WC_DateTime)#3745 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 15:05:40.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["date_modified"]=> object(WC_DateTime)#3742 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 16:02:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["discount_total"]=> string(2) "36" ["discount_tax"]=> string(1) "0" ["shipping_total"]=> string(1) "0" ["shipping_tax"]=> string(1) "0" ["cart_tax"]=> string(1) "0" ["total"]=> string(6) "325.30" ["total_tax"]=> string(1) "0" ["customer_id"]=> int(0) ["order_key"]=> string(22) "wc_order_5bdb16e89ce36" ["billing"]=> array(11) { ["first_name"]=> string(6) "Faizan" ["last_name"]=> string(5) "Zahid" ["company"]=> string(4) "Home" ["address_1"]=> string(6) "456789" ["address_2"]=> string(4) "5678" ["city"]=> string(6) "lahore" ["state"]=> string(2) "PB" ["postcode"]=> string(6) "456789" ["country"]=> string(2) "PK" ["email"]=> string(19) "fzahid001@gmail.com" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(6) "Tabish" ["last_name"]=> string(5) "Munir" ["company"]=> string(7) "Frizhub" ["address_1"]=> string(8) "34567890" ["address_2"]=> string(7) "4567890" ["city"]=> string(6) "lahore" ["state"]=> string(2) "PB" ["postcode"]=> string(6) "456789" ["country"]=> string(2) "PK" } ["payment_method"]=> string(5) "other" ["payment_method_title"]=> string(5) "other" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(5) "admin" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> object(WC_DateTime)#3790 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 15:15:33.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["cart_hash"]=> string(0) "" } ["items":protected]=> array(0) { } ["items_to_delete":protected]=> array(0) { } ["cache_group":protected]=> string(6) "orders" ["data_store_name":protected]=> string(5) "order" ["object_type":protected]=> string(5) "order" ["id":protected]=> int(983) ["changes":protected]=> array(0) { } ["object_read":protected]=> bool(true) ["extra_data":protected]=> array(0) { } ["default_data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(0) "" ["currency"]=> string(0) "" ["version"]=> string(0) "" ["prices_include_tax"]=> bool(false) ["date_created"]=> NULL ["date_modified"]=> NULL ["discount_total"]=> int(0) ["discount_tax"]=> int(0) ["shipping_total"]=> int(0) ["shipping_tax"]=> int(0) ["cart_tax"]=> int(0) ["total"]=> int(0) ["total_tax"]=> int(0) ["customer_id"]=> int(0) ["order_key"]=> string(0) "" ["billing"]=> array(11) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" ["email"]=> string(0) "" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" } ["payment_method"]=> string(0) "" ["payment_method_title"]=> string(0) "" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(0) "" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> NULL ["cart_hash"]=> string(0) "" } ["data_store":protected]=> object(WC_Data_Store)#3734 (4) { ["instance":"WC_Data_Store":private]=> object(WC_Order_Data_Store_CPT)#3804 (3) { ["internal_meta_keys":protected]=> array(78) { [0]=> string(10) "_parent_id" [1]=> string(7) "_status" [2]=> string(9) "_currency" [3]=> string(8) "_version" [4]=> string(19) "_prices_include_tax" [5]=> string(13) "_date_created" [6]=> string(14) "_date_modified" [7]=> string(15) "_discount_total" [8]=> string(13) "_discount_tax" [9]=> string(15) "_shipping_total" [10]=> string(13) "_shipping_tax" [11]=> string(9) "_cart_tax" [12]=> string(6) "_total" [13]=> string(10) "_total_tax" [14]=> string(12) "_customer_id" [15]=> string(10) "_order_key" [16]=> string(8) "_billing" [17]=> string(9) "_shipping" [18]=> string(15) "_payment_method" [19]=> string(21) "_payment_method_title" [20]=> string(15) "_transaction_id" [21]=> string(20) "_customer_ip_address" [22]=> string(20) "_customer_user_agent" [23]=> string(12) "_created_via" [24]=> string(14) "_customer_note" [25]=> string(15) "_date_completed" [26]=> string(10) "_date_paid" [27]=> string(10) "_cart_hash" [28]=> string(14) "_customer_user" [29]=> string(10) "_order_key" [30]=> string(15) "_order_currency" [31]=> string(19) "_billing_first_name" [32]=> string(18) "_billing_last_name" [33]=> string(16) "_billing_company" [34]=> string(18) "_billing_address_1" [35]=> string(18) "_billing_address_2" [36]=> string(13) "_billing_city" [37]=> string(14) "_billing_state" [38]=> string(17) "_billing_postcode" [39]=> string(16) "_billing_country" [40]=> string(14) "_billing_email" [41]=> string(14) "_billing_phone" [42]=> string(20) "_shipping_first_name" [43]=> string(19) "_shipping_last_name" [44]=> string(17) "_shipping_company" [45]=> string(19) "_shipping_address_1" [46]=> string(19) "_shipping_address_2" [47]=> string(14) "_shipping_city" [48]=> string(15) "_shipping_state" [49]=> string(18) "_shipping_postcode" [50]=> string(17) "_shipping_country" [51]=> string(15) "_completed_date" [52]=> string(10) "_paid_date" [53]=> string(10) "_edit_lock" [54]=> string(10) "_edit_last" [55]=> string(14) "_cart_discount" [56]=> string(18) "_cart_discount_tax" [57]=> string(15) "_order_shipping" [58]=> string(19) "_order_shipping_tax" [59]=> string(10) "_order_tax" [60]=> string(12) "_order_total" [61]=> string(15) "_payment_method" [62]=> string(21) "_payment_method_title" [63]=> string(15) "_transaction_id" [64]=> string(20) "_customer_ip_address" [65]=> string(20) "_customer_user_agent" [66]=> string(12) "_created_via" [67]=> string(14) "_order_version" [68]=> string(19) "_prices_include_tax" [69]=> string(15) "_date_completed" [70]=> string(10) "_date_paid" [71]=> string(15) "_payment_tokens" [72]=> string(22) "_billing_address_index" [73]=> string(23) "_shipping_address_index" [74]=> string(15) "_recorded_sales" [75]=> string(29) "_recorded_coupon_usage_counts" [76]=> string(29) "_download_permissions_granted" [77]=> string(20) "_order_stock_reduced" } ["meta_type":protected]=> string(4) "post" ["object_id_field_for_meta":protected]=> string(0) "" } ["stores":"WC_Data_Store":private]=> array(20) { ["coupon"]=> string(24) "WC_Coupon_Data_Store_CPT" ["customer"]=> string(22) "WC_Customer_Data_Store" ["customer-download"]=> string(31) "WC_Customer_Download_Data_Store" ["customer-download-log"]=> string(35) "WC_Customer_Download_Log_Data_Store" ["customer-session"]=> string(30) "WC_Customer_Data_Store_Session" ["order"]=> string(23) "WC_Order_Data_Store_CPT" ["order-refund"]=> string(30) "WC_Order_Refund_Data_Store_CPT" ["order-item"]=> string(24) "WC_Order_Item_Data_Store" ["order-item-coupon"]=> string(31) "WC_Order_Item_Coupon_Data_Store" ["order-item-fee"]=> string(28) "WC_Order_Item_Fee_Data_Store" ["order-item-product"]=> string(32) "WC_Order_Item_Product_Data_Store" ["order-item-shipping"]=> string(33) "WC_Order_Item_Shipping_Data_Store" ["order-item-tax"]=> string(28) "WC_Order_Item_Tax_Data_Store" ["payment-token"]=> string(27) "WC_Payment_Token_Data_Store" ["product"]=> string(25) "WC_Product_Data_Store_CPT" ["product-grouped"]=> string(33) "WC_Product_Grouped_Data_Store_CPT" ["product-variable"]=> string(34) "WC_Product_Variable_Data_Store_CPT" ["product-variation"]=> string(35) "WC_Product_Variation_Data_Store_CPT" ["shipping-zone"]=> string(27) "WC_Shipping_Zone_Data_Store" ["webhook"]=> string(21) "WC_Webhook_Data_Store" } ["current_class_name":"WC_Data_Store":private]=> string(23) "WC_Order_Data_Store_CPT" ["object_type":"WC_Data_Store":private]=> string(5) "order" } ["meta_data":protected]=> array(5) { [0]=> object(WC_Meta_Data)#3792 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2671) ["key"]=> string(17) "_vc_post_settings" ["value"]=> array(1) { ["vc_grid_id"]=> array(0) { } } } ["data":protected]=> array(3) { ["id"]=> int(2671) ["key"]=> string(17) "_vc_post_settings" ["value"]=> array(1) { ["vc_grid_id"]=> array(0) { } } } } [1]=> object(WC_Meta_Data)#3793 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2718) ["key"]=> string(14) "slide_template" ["value"]=> string(7) "default" } ["data":protected]=> array(3) { ["id"]=> int(2718) ["key"]=> string(14) "slide_template" ["value"]=> string(7) "default" } } [2]=> object(WC_Meta_Data)#3795 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2719) ["key"]=> string(19) "_bewpi_invoice_date" ["value"]=> string(19) "2018-11-01 15:08:31" } ["data":protected]=> array(3) { ["id"]=> int(2719) ["key"]=> string(19) "_bewpi_invoice_date" ["value"]=> string(19) "2018-11-01 15:08:31" } } [3]=> object(WC_Meta_Data)#3794 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2720) ["key"]=> string(21) "_bewpi_invoice_number" ["value"]=> string(3) "983" } ["data":protected]=> array(3) { ["id"]=> int(2720) ["key"]=> string(21) "_bewpi_invoice_number" ["value"]=> string(3) "983" } } [4]=> object(WC_Meta_Data)#3797 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2721) ["key"]=> string(23) "_bewpi_invoice_pdf_path" ["value"]=> string(19) "2018/00983-2018.pdf" } ["data":protected]=> array(3) { ["id"]=> int(2721) ["key"]=> string(23) "_bewpi_invoice_pdf_path" ["value"]=> string(19) "2018/00983-2018.pdf" } } } }

回答1:


You can use a custom field to keep the order status history. Then you will use the following hooked functions that will:

  • start registering pending status on order creation once the order has been placed by customer,
  • update the history each time the status change.

The code:

// 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:

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

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

You still need:

  • to set the correct date format (easy to set using the date() documentation)
  • to set the correct time zone in both first functions
  • to set the correct html structure output in the shortcode function.
  • to style as desired adding some CSS rules to your theme's styles.css file

USAGE:

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

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



来源:https://stackoverflow.com/questions/53105729/keep-and-get-order-status-history-with-their-time-stamps-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!