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

时间:2018-05-10 14:08:52

标签: php arrays wordpress woocommerce custom-fields

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

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

'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:"jon@freshysites.com";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:"example@gmail.com";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";}}}}'

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

  • WooCommerce订单电子邮件
  • 在后端订单表格屏幕中(作为新列)
  • 查看/编辑订单时,在“订单详细信息”中
  • ,以便更容易看到数据(例如,在结算或发货详细信息下方)

我希望能够从数组中获取某些元数据,例如WooCommerceEventsAttendeeNameWooCommerceEventsAttendeeLastNamefooevents_custom_state/provincefooevents_custom_addressfooevents_custom_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;
}

更新

我通过使用

为特定的WooCommerce订单回应了该键的元值
echo '<pre>'; print_r( get_post_meta( '4609', 'WooCommerceEventsOrderTickets', true ) ); echo '</pre>';

这就是显示:

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

                    [WooCommerceEventsVariationID] => 4078
                    [WooCommerceEventsPrice] => $675.00
                    [WooCommerceEventsPurchaserFirstName] => Jon
                    [WooCommerceEventsPurchaserLastName] => Fuller
                    [WooCommerceEventsPurchaserEmail] => freshyjon@gmail.com
                    [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
                        )

                )

        )

)

2 个答案:

答案 0 :(得分:2)

尝试类似下面的内容(因为我无法真正测试它,你可以安排它):

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文件中。它应该工作。

你会得到类似的东西:

enter image description here

答案 1 :(得分:0)

您发布的元数据似乎是序列化数组。看起来你应该只是能够反序列化它,然后遍历数组来检索你想要的值。

$meta = get_post_meta( $order->id, 'meta_key', true );
$unserialized = unserialize($meta);
foreach ($unserialized as $key => $value) {
   // Isolate values
}