Вы можете использовать настраиваемое поле для хранения истории статуса заказа. Тогда вы будете использовать следующие перехваченные функции, которые будут:
- начать регистрацию статуса ожидания при создании заказа, как только заказ был размещен клиентом,
- обновлять историю каждый раз при изменении статуса.
Код:
// 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
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.
Следующий шорткод выведет различные статусы заказа и временные метки:
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 вашей активной дочерней темы (или активной темы). Проверено и работает.
Вам все еще нужно:
- для установки правильного формата даты (легко установить с помощью
date()
документации )
- для установки правильного часового пояса в обеих первых функциях
- для установки правильного вывода структуры html в функцию шорткода.
- для стилизации по желанию, добавив некоторые правила CSS в файл styles.css вашей темы
ИСПОЛЬЗОВАНИЕ:
1) В текстовом редакторе WordPress сообщения или страницы: [status_history]
2) В коде PHP: echo do_shortcode( "[status_history]" );