在WooCommerce电子邮件订单元数据中获取自定义字段数组值 [英] Get a custom field array values within WooCommerce email order meta
问题描述
我正在使用FooEvents插件来捕获有关WooCommerce事件产品的事件参与者信息。下订单时,该插件会将与会者信息作为自定义字段数组保存到WooCommerce订单元。
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.
使用Post Meta Inspector插件,我能够看到与会者信息被保存为 WooCommerceEventsOrderTickets
的元键,并且值为:
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:未付费; 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数量>< 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; }}}}'
我希望获取数组中的值,以使用该信息-以便显示某些字段值:
I am looking to grab the values within the array, to use that information — to be able to display certain field values within:
- WooCommerce订单电子邮件 在后端订单表屏幕(作为新列)中的
- 在查看/编辑订单时,在订单明细中的
- 更容易看到(例如,在帐单或运输详细信息下方)
- WooCommerce order emails
- in the backend Orders table screen (as a new column)
- within the "Order details" when viewing/editing the order, so that the data is more easily visible (e.g., below the Billing or Shipping details)
我希望能够从数组,例如 WooCommerceEventsAttendeeName
, WooCommerceEventsAttendeeLastName
, fooevents_custom_state / province
, fooevents_custom_address
, fooevents_custom_title
等
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.
此PHP片段将在订购电子邮件中添加一个字段,但是我如何对其进行编辑,以便可以显示该数组中的某些元数据?
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;
}
更新:
对于特定的WooCommerce订单,我已经通过使用
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>';
这是显示的内容:
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
)
)
)
)
推荐答案
尝试执行以下操作(因为我无法真正对其进行测试,您可能应该安排它):
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;
}
代码进入活动子主题(或活动主题)的function.php文件)。
Code goes in function.php file of your active child theme (or active theme). It should work.
您将得到类似的东西:
这篇关于在WooCommerce电子邮件订单元数据中获取自定义字段数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!