在WooCommerce订单和电子邮件通知中显示自定义字段值

时间:2019-03-12 06:19:23

标签: php wordpress woocommerce checkout orders

基于显示自定义取件字段和交货日期的“ Choosing a date and time after choosing the WooCommerce delivery method”应答代码,以下代码在订单编辑页面上显示这些字段的交货数据。

这是我的代码:

// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_fields_order_meta', 10, 1 );
function my_custom_fields_order_meta($order){   

    $delivery_option = $order->get_meta('_delivery_option');

    if( $delivery_option == 'date' ) {
    $delivery_datetime = $order->get_meta('_delivery_datetime');

    echo '<p><strong>'.__('Delivery').':</strong> ' . get_post_meta( $order->id, '_delivery_option', true ) . '</p>';
    echo '<p><strong>'.__('Delivery Date').':</strong> ' . get_post_meta( $order->id, '_delivery_datetime', true ) . '</p>';
    }
}

不幸的是,仅正确显示了客户选择的交货日期,并且未显示“尽快”单选按钮的选项。

显然,我做错了。

我还要在“谢谢”页面和电子邮件中显示这些字段的值。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

要在后端订单编辑页面中显示自定义字段值(如果它们已保存在订单的数据库中),请使用以下命令:

    { status: 202,
  statusText: 'Accepted',
  headers:
   { server: 'Server',
     date: 'Tue, 12 Mar 2019 20:34:36 GMT',
     'content-type': 'application/json',
     'content-length': '61',
     'x-amzn-requestid': '400377cd-4506-11e9-b941-3984677a719c',
     'x-amz-apigw-id': 'WciqVH8pPHcFgWg=',
     location:
      '/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974
     'x-amzn-trace-id': 'Root=1-5c8817db-5522b10bf2068ac2509ae35b;Samp
     connection: 'close' },
  config:
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers:
      { Accept: 'application/json',
        'Content-Type': 'application/json',
        'x-apigw-api-id': 'vs2i50xvo4',
        'x-amz-date': '20190312T203427Z',
        Authorization:
         'AWS4-HMAC-SHA256 Credential=AKIAJ4Y5DGRDNBQ2HBYA/20190312/us
cept;content-type;host;x-amz-date;x-apigw-api-id, Signature=801d0887fd
a249',
        'User-Agent': 'axios/0.18.0',
        'Content-Length': 245 },
     method: 'post',
     data:
      '{"site_id":"TDCLOUDTSTTD03","account_id":"109443356002","change
ider":"aws","region":"us-west-2","network_id":"vpc-0ab26280d5060105c",
01.128/25"]}',
     url:
      'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.v
ites' },
  request:
   ClientRequest {
     _events:
      [Object: null prototype] {
        socket: [Function],
        abort: [Function],
        aborted: [Function],
        error: [Function],
        timeout: [Function],
        prefinish: [Function: requestOnPrefinish] },
     _eventsCount: 6,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: true,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: true,
     sendDate: false,
     _removedConnection: false,
     _removedContLen: false,
     _removedTE: false,
     _contentLength: null,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket:
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername:
         'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 8,
        connecting: false,
        _hadError: false,
        _handle: [TLSWrap],
        _parent: null,
        _host:
         'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
        _readableState: [ReadableState],
        readable: true,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [TLSWrap],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(res)]: [TLSWrap],
        [Symbol(asyncId)]: 6,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(connect-options)]: [Object] },
     connection:
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername:
         'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 8,
        connecting: false,
        _hadError: false,
        _handle: [TLSWrap],
        _parent: null,
        _host:
         'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
        _readableState: [ReadableState],
        readable: true,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [TLSWrap],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(res)]: [TLSWrap],
        [Symbol(asyncId)]: 6,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(connect-options)]: [Object] },
     _header:
      'POST /qa/transit-connectivity/api/v1/sites HTTP/1.1\r\nAccept:
\r\nx-apigw-api-id: vs2i50xvo4\r\nx-amz-date: 20190312T203427Z\r\nAuth
DNBQ2HBYA/20190312/us-west-2/execute-api/aws4_request, SignedHeaders=a
 Signature=801d0887fd4a8b86a1af51a02cede529962fc5cae84f4a10f23983100e0
h: 245\r\nHost: vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.
     _onPendingData: [Function: noopPendingOutput],
     agent:
      Agent {
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        maxCachedSessions: 100,
        _sessionCache: [Object] },
     socketPath: undefined,
     timeout: undefined,
     method: 'POST',
     path: '/qa/transit-connectivity/api/v1/sites',
     _ended: true,
     res:
      IncomingMessage {
        _readableState: [ReadableState],
        readable: false,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        socket: [TLSSocket],
        connection: [TLSSocket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Array],
        trailers: {},
        rawTrailers: [],
        aborted: false,
        upgrade: false,
        url: '',
        method: null,
        statusCode: 202,
        statusMessage: 'Accepted',
        client: [TLSSocket],
        _consuming: true,
        _dumped: false,
        req: [Circular],
        responseUrl:
         'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites',
        redirects: [] },
     aborted: undefined,
     timeoutCb: null,
     upgradeOrConnect: false,
     parser: null,
     maxHeadersCount: null,
     _redirectable:
      Writable {
        _writableState: [WritableState],
        writable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _options: [Object],
        _redirectCount: 0,
        _redirects: [],
        _requestBodyLength: 245,
        _requestBodyBuffers: [],
        _onNativeResponse: [Function],
        _currentRequest: [Circular],
        _currentUrl:
         'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites' },
     [Symbol(isCorked)]: false,
     [Symbol(outHeadersKey)]:
      [Object: null prototype] {
        accept: [Array],
        'content-type': [Array],
        'x-apigw-api-id': [Array],
        'x-amz-date': [Array],
        authorization: [Array],
        'user-agent': [Array],
        'content-length': [Array],
        host: [Array] } },
  data: { status: 'success', message: 'request has been accepted' } }

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here


前端订单页面电子邮件通知上各处显示自定义字段值的最好的简洁方法是在订单总计表中显示它们,就像用过的付款一样方法:

// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_fields_value_admin_order', 10, 1 );
function display_custom_fields_value_admin_order( $order ){
    // Display the delivery option
    if( $delivery_option =  $order->get_meta('_delivery_option') )
        echo '<p><strong>'.__('Delivery type').':</strong> ' . $delivery_option . '</p>';

    // Display the delivery date
    if( $delivery_datetime = $order->get_meta('_delivery_datetime') )
        echo '<p><strong>'.__('Delivery Date').':</strong> ' . $delivery_datetime . '</p>';
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here


相关主题: Choosing a date and time after choosing the WooCommerce delivery method