Show time on order backend broken since Woo 3
-
Hi All! I used to use the below function to show the time on orders in the backend. Does anyone have a fix for this?
// Woocommerce show time on order add_filter('post_date_column_time', 'custom_post_date_column_time', 10, 2); function custom_post_date_column_time($h_time, $post) { return get_the_time(__('Y/m/d g:i:s A', 'woocommerce'), $post); }
-
This filter does not exist since moving to the new crud – using $post is avoided.
@mikejolley, What alternative method would I have to be able to achieve the above?
Thanks! @mikejolley
For ease of reference for others, the new update to the core code is due to be pulling into the 3.0.2 update.
To manually apply the patch, you have to edit /wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php and replace:
printf( '<time datetime="%s">%s</time>', esc_attr( $the_order->get_date_created()->date( 'c' ) ), esc_html( $the_order->get_date_created()->date_i18n( __( 'Y-m-d', 'woocommerce' ) ) ) );
With this:
printf( '<time datetime="%s">%s</time>', esc_attr( $the_order->get_date_created()->date( 'c' ) ), esc_html( $the_order->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'Y-m-d', 'woocommerce' ) ) ) ) );'
The following filter works:
// Woocommerce show time on order add_filter('woocommerce_admin_order_date_format', 'custom_post_date_column_time'); function custom_post_date_column_time($h_time, $post) { return get_the_time(__('Y/m/d G:i', 'woocommerce'), $post); }
-
This reply was modified 7 years, 11 months ago by
bruceappinletcom.
-
This reply was modified 7 years, 11 months ago by
bruceappinletcom.
For ease of reference for others, the new filter is due to be released in WooCommerce 3.0.2.
Patch can be manually applied from https://github.com/woocommerce/woocommerce/pull/14253
The below snippet works:
// Woocommerce show time on order add_filter('woocommerce_admin_order_date_format', 'custom_post_date_column_time'); function custom_post_date_column_time($h_time, $post) { return get_the_time(__('Y/m/d G:i', 'woocommerce'), $post); }
-
This reply was modified 7 years, 11 months ago by
bruceappinletcom.
While using the above snippet still works, I’m now getting this notice in my debug log:
PHP Notice: Undefined variable: post
Now the above snippet gives:
Warning: Missing argument 2 for custom_post_date_column_time(), called in /xxx/wp-includes/class-wp-hook.php on line 298 and defined in /xxx/wp-content/themes/yyyy/functions.php on line 40
So the snippet does not seem to work anymore.
If I delete all mentions of $post, it works without error.
So the final code becomes:
// Woocommerce show time on order add_filter('woocommerce_admin_order_date_format', 'custom_post_date_column_time'); function custom_post_date_column_time($h_time) { return get_the_time(__('Y/m/d G:i', 'woocommerce')); }
Or actually it screws my backend up. Can a smarter person enlighten us?
Based off another snippet I’ve been using, I wrote the below as an alternative workaround.
/* * Show order time as an custom column on backend */ function appinlet_wc_custom_time_column($columns) { $new_array = array(); foreach ($columns as $key => $title) { if ($key == 'order_total') { $new_array['order_time'] = __( 'Time','time-items-column-woocommerce'); } $new_array[$key] = $title; } return $new_array; } add_filter('manage_edit-shop_order_columns', 'appinlet_wc_custom_time_column'); /* process custom columns */ function appinlet_wc_shop_custom_column($column) { if ($column == 'order_items') { echo '<a href="#" class="show_order_items" data-wc-order="'.get_the_ID().'">Show items</a><div id="show_order_items_'.get_the_ID().'"></div>'; } if ($column == 'order_time') { echo '<a href="#" class="show_order_time" data-wc-order="'.get_the_ID().'">Show time</a><div id="show_order_time_'.get_the_ID().'"></div>'; } } add_action('manage_shop_order_posts_custom_column', 'appinlet_wc_shop_custom_column', 10, 2); function appinlet_wc_find_time_ajax_javascript() { if (isset($_GET["post_type"]) && ($_GET["post_type"] != 'shop_order')) { return; } ?> <script> jQuery(document).ready(function($) { $(".show_order_time").click(function() { var order_id = $(this).data("wc-order"); $('#show_order_time_'+order_id).html('loading...'); var data = { 'action': 'appinlet_wc_find_time_ajax', 'order_id': order_id }; $.post(ajaxurl, data, function(response) { $('#show_order_time_'+order_id).html(response); }); }); }); </script> <?php } add_action( 'admin_head-edit.php', 'appinlet_wc_find_time_ajax_javascript' ); function appinlet_wc_find_time_ajax() { $post_id = intval( $_POST['order_id'] ); $post = get_post( $post_id ); $time = get_the_time(__('G:i', 'woocommerce'), $post); echo $time; wp_die(); } add_action( 'wp_ajax_appinlet_wc_find_time_ajax', 'appinlet_wc_find_time_ajax' );
The way the filter works has changed in Woo 3.x.
Instead of filtering the entire date itself, the filter is now just on the date format string used by PHP. There is no second $post argument passed in, which is what the error you’re seeing is. And instead of returning an actual date, it’s just supposed to return the date format string.
In other words, instead of returning a string like “2017/11/18 09:01 AM” , your filter function now needs to return a string like: “Y/m/d h:i A” and WooCommerce is responsible for looking up the date on the post.
Here is a fixed implementation that works for me, it’s much simpler now!
// show timestamp on orders screen, instead of just the date. add_filter('woocommerce_admin_order_date_format', 'custom_post_date_column_time'); function custom_post_date_column_time($format) { // Any PHP date format string will work for the 'Y/m/d h:i A' part. // see the PHP docs at: https://php.net/manual/en/function.date-format.php return __('Y/m/d h:i A', 'woocommerce'); }
-
This reply was modified 7 years, 11 months ago by
- The topic ‘Show time on order backend broken since Woo 3’ is closed to new replies.