Skip Navigation

[Resolved] How to format woocommerce _completed_date hidden custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display the WooCommerce Order's completed date, but the hidden custom field _completed_date is not formatted as I would like.

Solution:
Add this custom shortcode to your functions.php file:

function format_completed_date_func($atts) {
  $a = shortcode_atts( array(
    'format' => 'm d Y',
    'orderid' => 0
  ), $atts );
 
  $completedDate = get_post_meta( $a['orderid'], '_completed_date', true );
  if ( !$completedDate )
    return;
  $time = strtotime($completedDate);
  $date = date($a['format'], $time);
  return $date;
}
 
add_shortcode( 'format_completed_date', 'format_completed_date_func');

Use the custom shortcode in a View's loop like this:

[format_completed_date format='m/d/Y' orderid='[wpv-post-id]']

Replace m/d/Y with the PHP date formatting of your choice.

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post_meta
https://codex.wordpress.org/Shortcode_API
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by Nicholas 4 years, 4 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#827358

Hello I'd like to format the woocommerce _completed_date hidden custom field

Right now the date of the completed order is displayed as such 2018-05-05 13:28:24 and I would like to display it as 05/05/18.

How can I format this date?

Regards,
Nick

#836697

The field _completed_date is formatted as a string, so you would have to write custom PHP code to translate that string into a PHP date, then reformat it however you want. I'm not sure how you're displaying it on the front-end, but this is how it could be accomplished on the back-end:

$string = '2018-05-06 21:10:16'; // your _completed_date value
$format = 'm d Y'; // the PHP date format you want to use
$time = strtotime($string);
$date = date( $format, $time);
return $date;

http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php

#864925

Hello and thank you.

I'd like to use the shortcode in a view that outputs all of the woocommerce orders on the frontend.

So does that mean I'd have to create a custom shortcode?

Best,
Nick

#867568

Yes, something like this:

function format_completed_date_func($atts) {
  $a = shortcode_atts( array(
    'format' => 'm d Y',
    'orderid' => 0
  ), $atts );

  $completedDate = get_post_meta( $a['orderid'], '_completed_date', true );
  if ( !$completedDate )
    return;
  $time = strtotime($completedDate);
  $date = date($a['format'], $time);
  return $date;
}

add_shortcode( 'format_completed_date', 'format_completed_date_func');

Then you can use the shortcode in the View's loop like this (replace m/d/Y with the date format of your choice):

[format_completed_date format='m/d/Y' orderid='[wpv-post-id]']
#868704

Thank you. It works like a charm.