'From wc_get_order() to REST API data - WooCommerce
Comparing the data returned by wc_get_order($order)->get_data() to the data returned by the wc()->api->get_endpoint_data('/wc/v3/orders/' . $order); REST endpoint, the data is the same, except for that in the first case, for some keys the data is an object instead of a formatted piece of information.
E.g. [date_created] => 2022-04-13T10:11:52 returned by REST, is like this
[date_created] => WC_DateTime Object
(
[utc_offset:protected] => 7200
[date] => 2022-04-13 08:11:52.000000
[timezone_type] => 1
[timezone] => +00:00
)
in the data returned by wc_get_order()...
What's the code that "flattens" the data converting it to the one returned by the REST API?
Solution 1:[1]
/**
* Parses and formats a date for ISO8601/RFC3339.
*
* Required WP 4.4 or later.
* See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/
*
* @since 2.6.0
* @param string|null|WC_DateTime $date Date.
* @param bool $utc Send false to get local/offset time.
* @return string|null ISO8601/RFC3339 formatted datetime.
*/
function wc_rest_prepare_date_response( $date, $utc = true ) {
if ( is_numeric( $date ) ) {
$date = new WC_DateTime( "@$date", new DateTimeZone( 'UTC' ) );
$date->setTimezone( new DateTimeZone( wc_timezone_string() ) );
} elseif ( is_string( $date ) ) {
$date = new WC_DateTime( $date, new DateTimeZone( 'UTC' ) );
$date->setTimezone( new DateTimeZone( wc_timezone_string() ) );
}
if ( ! is_a( $date, 'WC_DateTime' ) ) {
return null;
}
// Get timestamp before changing timezone to UTC.
return gmdate( 'Y-m-d\TH:i:s', $utc ? $date->getTimestamp() : $date->getOffsetTimestamp() );
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mujuonly |
