从wc_get_order()到REST API数据 - WooCommerce

发布于 2025-01-27 08:32:37 字数 596 浏览 4 评论 0原文

wc_get_order返回的数据($ order) - > get_data()wc() - > gt; api-> get_endpoint_data('//wc/v3/v3/v3/v3/v3/get_data()订单/'$订单); 休息端点,数据是相同的,除了第一个情况下,对于某些键,数据是一个对象而不是格式化的信息。

例如[date_created] => 2022-04-13T10:11:52由REST返回,

[date_created] => WC_DateTime Object
    (
        [utc_offset:protected] => 7200
        [date] => 2022-04-13 08:11:52.000000
        [timezone_type] => 1
        [timezone] => +00:00
    )

wc_get_order()返回的数据中,

是什么“变平”将其转换为数据的代码是什么其余API返回的那个?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2025-02-03 08:32:37
/**
 * 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() );
}
/**
 * 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() );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文