WooCommerce 3.0+ change admin order date column format

限于喜欢 提交于 2019-12-08 04:16:08

问题


In WooCommerce I was using the code below to alter the admin orders view formatting of the order date column:

// 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);
}

Since WooCommerce 3.0+ it has just stopped working.

Any ideas?

Thanks


回答1:


In WC core code class-wc-admin-post-types.php if you look at render_shop_order_columns() function, things have changed since WooCommerce version 3.0+, as it uses WC_Abstract_Order get_date_created() method instead of WordPress function get_the_time().

So that is why the hook that you are using is not working anymore.

Here is an extract of the source code in WooCommerce version 3.0+:

case 'order_date' :
    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' ) ) ) );
break;

And Here the same source code extract in WooCommerce version 2.6.x:

case 'order_date' :

    if ( '0000-00-00 00:00:00' == $post->post_date ) {
        $t_time = $h_time = __( 'Unpublished', 'woocommerce' );
    } else {
        $t_time = get_the_time( __( 'Y/m/d g:i:s A', 'woocommerce' ), $post );
        $h_time = get_the_time( __( 'Y/m/d', 'woocommerce' ), $post );
    }

    echo '<abbr title="' . esc_attr( $t_time ) . '">' . esc_html( apply_filters( 'post_date_column_time', $h_time, $post ) ) . '</abbr>';

break;

Now if you look to the source code of get_date_created() it's using the new WC_Data getter method get_prop(). In the source code of get_prop() you have this new filter hook possibility to explore:

$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );



回答2:


THE RIGHT ANSWER:

Please see the official WooCommerce answer thread

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

It works with a custom function hooked in woocommerce_admin_order_date_format new filter hook:

// 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);
}


来源:https://stackoverflow.com/questions/43331509/woocommerce-3-0-change-admin-order-date-column-format

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