Php 在WooCommerce电子邮件订单元数据中获取自定义字段数组值

Php 在WooCommerce电子邮件订单元数据中获取自定义字段数组值,php,arrays,wordpress,woocommerce,custom-fields,Php,Arrays,Wordpress,Woocommerce,Custom Fields,我正在使用FooEvents插件捕获WooCommerce活动产品的活动参与者信息。下订单时,该插件将与会者信息作为自定义字段数组保存到WooCommerce订单元中 使用Post Meta Inspector插件,我可以看到与会者信息被保存为WooCommerceEvents或DerTickets的元键,值为: 'a:1:{i:1;a:1:{i:1;a:18:{s:26:“WooCommerceEventsProductID”;i:454;s:24:“WooCommerceEventsOrde

我正在使用FooEvents插件捕获WooCommerce活动产品的活动参与者信息。下订单时,该插件将与会者信息作为自定义字段数组保存到WooCommerce订单元中

使用Post Meta Inspector插件,我可以看到与会者信息被保存为
WooCommerceEvents或DerTickets
的元键,值为:

'a:1:{i:1;a:1:{i:1;a:18:{s:26:“WooCommerceEventsProductID”;i:454;s:24:“WooCommerceEventsOrderID”;i:4609;s:27:“WooCommerceEventsSticketType”;s:0:;s:23:“WooCommerceEventsStatus”;s:6:“未付”;s:27:“WooCommerceEventsCustomersCustomerId”;s:2:“64”;s:29:“WooCommerceEventsAtendeName”;s:13:“与会者第一”;33:“WooCommerceEventsAtendesAtendes:12:“与会者最后”s:30:“WooCommerceEventsAttendeeEmail”;s:19:jon@freshysites.com“s:34:”WooCommerceEventsAttendeeTelephone“;s:12:”607-123-4567“;s:32:”WooCommerceEventsAttendeeCompany“;s:0:”s:36:”WooCommerceEventsAttendeSignation“;s:27:”WooCommerceEventsVariations“;a:1:{s:33:”属性注册选项“;s:14:”nassgap成员“}:28:”WooCommerceEventsVariationID;i:4078;s:22:“WooCommerceEventsSprice”;s:118:“675.00美元”;s:35:“WooCommerceEventsSpurchaserFirstName”;s:3:“Jon”;s:34:“WooCommerceEventsSpurchaserLastName”;s:6:“Fuller”;s:31:“WooCommerceEventsSpurchasesRemail”;s:19:“example@gmail.com“s:37:“WooCommerceEventsToestendeFields”;a:6:{s:24:“fooevents\u custom\u address”;s:15:”123与会者St;s:21:“fooevents\u custom\u city”s:13:“与会者city”s:31“fooevents\u custom\u state/province”s:6“内华达州”s:28“fooevents\u custom\u邮政编码”s:5:“13813”s:29“fooevents\u custom\u organization”s:12“Attendee LLC”s:22“fooevents\u custom\u title”s:14“与会者头衔”}}”

我希望获取数组中的值,使用该信息-能够显示以下字段中的某些字段值:

  • WOO商业订单电子邮件
  • 在后端订单表屏幕中(作为新列)
  • 查看/编辑订单时,在“订单详细信息”中,以便更容易看到数据(例如,账单或装运详细信息下方)
我希望能够从数组中获取某些元值,例如
woocmerceeventsatteneName
woocmerceeventsatteneelastname
fooevents\u custom\u state/province
fooevents\u custom\u address
fooevents\u custom\u title
,等等

这个PHP代码片段将在订单电子邮件中添加一个字段,但是如何编辑它,以便让它显示该数组中的一些元数据

 /**
 * Add a custom field (in an order) to the emails
 */
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key', true ),
    );
    return $fields;
}
更新: 我已经通过使用对该键的元值进行了响应,以用于特定的商业订单

echo '<pre>'; print_r( get_post_meta( '4609', 'WooCommerceEventsOrderTickets', true ) ); echo '</pre>';

您发布的元数据似乎是一个序列化的数组。似乎您应该能够将其取消序列化,然后在数组中循环以检索所需的值

add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $event = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );

    if( ! is_array($event) ) return $fields;

    $event = isset($event[1][1]) ? $event[1][1] : '';

    if( sizeof($event) == 0 ) return $fields;

    $custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';

    // Set our array of needed data
    $fields_array = [
        __('First name')    => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
        __('Last name')     => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
        __('Title')         => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
        __('Organization')  => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
        __('Address')       => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
        __('City')          => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
        __('Postcode')      => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
        __('State')         => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
    ];

    // Loop though the data array to set the fields
    foreach( $fields_array as $label => $value ){
        if( ! empty($value) ){
            $fields[] = array(
                'label' => $label,
                'value' => $value,
            );
        }
    }

    return $fields;
}

尝试以下方法(因为我无法真正测试它,您可以安排它):

代码放在活动子主题(或活动主题)的function.php文件中。它应该可以工作

您将得到如下结果:


@LoicTheAztec是否有办法将其取消序列化?看起来这些数据也存储在其自己的post类型的事件\u magic\u票据中,其中还包括对原始订单号的引用:它存储在wp\u Posteta db表中:在
wp\u Posteta
表中,还有之前粘贴在此处的实际数组。
meta_键
woocmerceeventsordertickets
,每个帖子的
meta_值
都会发生变化(订单)。上面的问题中粘贴了一个示例。这是一个屏幕截图:这些post ID是WooCommerce订单。
print\u
不起作用,但
print\u r
起作用,如果这是你的意思的话。我已经用数组的输出更新了上面的问题。很抱歉,这是
print\u r
而不是
print\u
。因此这将是一个问题ch现在更有用了。谢谢。您可以更详细地描述一下如何呼出的值,例如
woocmerceevents satteneName
fooevents\u custom\u city
工作得很好。谢谢!关于能够将这些字段中的一些字段用作管理订单表中的列,有什么建议吗?另外,我如何添加
与会者信息
ab如果/当有字段时,是否查看字段输出?您可以用完全不同的方式执行所有操作,包括标题和类似的表格,如包含所有自定义数据的订单表格。但这是另一件不会出现在此处的注释中的事情…我添加了一个新问题:
$meta = get_post_meta( $order->id, 'meta_key', true );
$unserialized = unserialize($meta);
foreach ($unserialized as $key => $value) {
   // Isolate values
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $event = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );

    if( ! is_array($event) ) return $fields;

    $event = isset($event[1][1]) ? $event[1][1] : '';

    if( sizeof($event) == 0 ) return $fields;

    $custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';

    // Set our array of needed data
    $fields_array = [
        __('First name')    => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
        __('Last name')     => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
        __('Title')         => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
        __('Organization')  => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
        __('Address')       => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
        __('City')          => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
        __('Postcode')      => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
        __('State')         => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
    ];

    // Loop though the data array to set the fields
    foreach( $fields_array as $label => $value ){
        if( ! empty($value) ){
            $fields[] = array(
                'label' => $label,
                'value' => $value,
            );
        }
    }

    return $fields;
}