Garconis
Garconis

Reputation: 784

Get a custom field array values within WooCommerce email order meta

I am using the FooEvents plugin to capture event attendee information on WooCommerce event products. When an order is placed, that plugin is saving the attendee information to the WooCommerce order meta as a custom field Array.

Using the Post Meta Inspector plugin, I am able to see that the attendee information is being saved as a meta key of WooCommerceEventsOrderTickets and with a value of:

'a:1:{i:1;a:1:{i:1;a:18:{s:26:"WooCommerceEventsProductID";i:454;s:24:"WooCommerceEventsOrderID";i:4609;s:27:"WooCommerceEventsTicketType";s:0:"";s:23:"WooCommerceEventsStatus";s:6:"Unpaid";s:27:"WooCommerceEventsCustomerID";s:2:"64";s:29:"WooCommerceEventsAttendeeName";s:13:"AttendeeFirst";s:33:"WooCommerceEventsAttendeeLastName";s:12:"AttendeeLast";s:30:"WooCommerceEventsAttendeeEmail";s:19:"[email protected]";s:34:"WooCommerceEventsAttendeeTelephone";s:12:"607-123-4567";s:32:"WooCommerceEventsAttendeeCompany";s:0:"";s:36:"WooCommerceEventsAttendeeDesignation";s:0:"";s:27:"WooCommerceEventsVariations";a:1:{s:33:"attribute_pa_registration-options";s:14:"nassgap-member";}s:28:"WooCommerceEventsVariationID";i:4078;s:22:"WooCommerceEventsPrice";s:118:"<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>675.00</span>";s:35:"WooCommerceEventsPurchaserFirstName";s:3:"Jon";s:34:"WooCommerceEventsPurchaserLastName";s:6:"Fuller";s:31:"WooCommerceEventsPurchaserEmail";s:19:"[email protected]";s:37:"WooCommerceEventsCustomAttendeeFields";a:6:{s:24:"fooevents_custom_address";s:15:"123 Attendee St";s:21:"fooevents_custom_city";s:13:"Attendee City";s:31:"fooevents_custom_state/province";s:6:"Nevada";s:28:"fooevents_custom_postal_code";s:5:"13813";s:29:"fooevents_custom_organization";s:12:"Attendee LLC";s:22:"fooevents_custom_title";s:14:"Attendee Title";}}}}'

I am looking to grab the values within the array, to use that information — to be able to display certain field values within:

I would like to be able to grab certain meta values from the array, such as WooCommerceEventsAttendeeName, WooCommerceEventsAttendeeLastName, fooevents_custom_state/province, fooevents_custom_address, fooevents_custom_title, etc.

This PHP snippet will add a field to the Order Emails, but how to I edit this to that I can get it to display some of the meta from within that array?

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

Update:

I have echoed a the meta value of that key, for a particular WooCommerce order by using

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

This is what shows:

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [WooCommerceEventsProductID] => 454
                    [WooCommerceEventsOrderID] => 4609
                    [WooCommerceEventsTicketType] => 
                    [WooCommerceEventsStatus] => Unpaid
                    [WooCommerceEventsCustomerID] => 64
                    [WooCommerceEventsAttendeeName] => AttendeeFirst
                    [WooCommerceEventsAttendeeLastName] => AttendeeLast
                    [WooCommerceEventsAttendeeEmail] => [email protected]
                    [WooCommerceEventsAttendeeTelephone] => 607-123-4567
                    [WooCommerceEventsAttendeeCompany] => 
                    [WooCommerceEventsAttendeeDesignation] => 
                    [WooCommerceEventsVariations] => Array
                        (
                            [attribute_pa_registration-options] => nassgap-member
                        )

                    [WooCommerceEventsVariationID] => 4078
                    [WooCommerceEventsPrice] => $675.00
                    [WooCommerceEventsPurchaserFirstName] => Jon
                    [WooCommerceEventsPurchaserLastName] => Fuller
                    [WooCommerceEventsPurchaserEmail] => [email protected]
                    [WooCommerceEventsCustomAttendeeFields] => Array
                        (
                            [fooevents_custom_address] => 123 Attendee St
                            [fooevents_custom_city] => Attendee City
                            [fooevents_custom_state/province] => Nevada
                            [fooevents_custom_postal_code] => 13813
                            [fooevents_custom_organization] => Attendee LLC
                            [fooevents_custom_title] => Attendee Title
                        )

                )

        )

)

Upvotes: 1

Views: 2187

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

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:

enter image description here

Upvotes: 2

ethan
ethan

Reputation: 975

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
}

Upvotes: 0

Related Questions