在Woocommerce 3中获取PayTrace支付网关的订单自定义元数据

I set up 3 custom checkout fields using WooCommerce's plugin 'Checkout Field Editor.' I have it set so that the fields are displayed on the emails and on the order screen, but I need for the info in those fields to also be sent to the Payment Processor, which is PayTrace. After speaking with the PayTrace Plugin developer, I have the code (link below) for adding additional information order info that is sent to PayTrace but I am not sure how to retrieve custom checkout field data properly/safely so that it can then be included.

Code for adding additional data in information sent to PayTrace. https://gist.github.com/vanbo/dc3d9bc8660a7de33aafb44dde3033fa

In general Woocommerce Order meta data is mostly always prepended with an underscore character. For example the billing first name meta_key will be _billing_first_name.

For that reason you need first to check in wp_postmeta table for the last Woocommerce Order (post_id that is the order ID), to get the correct necessary meta keys that you will use in the following code.

Let say that your custom meta keys are:

  • _event_code for Event code
  • _event_date for Event date
  • _event_name for Event name

Once done, you will get and set that data in the following code, something like:

add_filter( 'wc_paytrace_transaction_request', 'custom_data_paytrace_transaction_request', 10, 5 );
function prefix_filter_paytrace_transaction_request( $request_parameters, $order, $amount, $is_subscription, $is_paid_with_profile ) {
    $event_code = $order->get_meta( '_event_code', true );
    if( ! empty($event_code) )
        $request_parameters['event_code'] = $event_code;

    $event_date = $order->get_meta( '_event_date', true );
    if( ! empty($event_date) )
        $request_parameters['event_date'] = $event_date;

    $event_name = $order->get_meta( '_event_name', true );
    if( ! empty($event_name) )
        $request_parameters['event_name'] = $event_name;

    return $request_parameters;
}

You should need to check that the array keys for $request_parameters are the correct ones to be used.

Code goes in function.php file of the active child theme (or active theme). It should works.