Get a custom field array values within WooCommerce email order meta

老子叫甜甜 提交于 2019-12-09 04:49:26
LoicTheAztec

Try something like the following (as I can't really test it, you may should arrange it):

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;
}

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

You will get something like:

The meta data you've posted appears to be a serialized array. It seems you should simply be able to unserialize it and then loop through the array to retrieve your desired values.

$meta = get_post_meta( $order->id, 'meta_key', true );
$unserialized = unserialize($meta);
foreach ($unserialized as $key => $value) {
   // Isolate values
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!