Здесь оба поля объединяются, сохраняются и отображаются в порядке уведомлений администратора и электронной почты:
// Get "hearaboutus" select field options data
function wc_get_hearaboutus_options(){
return array(
'' => 'Please select...',
'option_1' => 'Social Media (e.g Facebook)',
'option_2' => 'Search Engine (e.g Google)',
'option_3' => 'Meditation Class',
'option_4' => 'Leaflets/Flyers/Posters',
'option_5' => 'Website',
'option_6' => 'Email Newsletter',
'option_7' => 'Other',
);
}
// Add the fields to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">
<h3>' . __('Please help us understand our customers so that we can improve future events (Optional)') . '</h3>';
woocommerce_form_field( '_hearaboutus', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('How did you hear about us? '),
'options' => wc_get_hearaboutus_options(),
), $checkout->get_value( '_hearaboutus' ) );
echo '</div>';
echo '<div class="cw_custom_class">
<h2>'.__('Would you like to be added to the WhatsApp list?').'</h2>';
$whatsapp_list =
woocommerce_form_field( 'whatsapp_list', array(
'type' => 'checkbox',
'label' => __("Yes, add me to the list!", "woocommerce"),
'required' => false,
), $checkout->get_value( 'whatsapp_list' ) );
echo '</div>';
}
// Update the order meta with fields values
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_create_order', 10, 2 );
function custom_checkout_field_create_order( $order, $data ) {
if ( isset($_POST['_hearaboutus']) && ! empty($_POST['_hearaboutus']) ) {
$order->update_meta_data( '_hearaboutus', sanitize_text_field($_POST['_hearaboutus']) );
}
if ( isset($_POST['whatsapp_list']) ) {
$order->update_meta_data( '_whatsapp_list', 'Yes' );
}
}
// Add the fields to order email
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
// The data 1
$label1 = __('How did you hear about us?');
$value1 = wc_get_hearaboutus_options()[$hearaboutus];
}
if( $whatsapp_list = $order->get_meta('_whatsapp_list') ) {
// The data 1
$label2 = __('Add me to WhatsApp list?');
$value2 = $whatsapp_list;
}
if( isset($value1) && isset($value2) ){
// The HTML Structure
$html_output = '<h2>' . __('Extra data') . '</h2>
<div class="discount-info"><table cellspacing="0" cellpadding="6">';
if( isset($value1) ){
$html_output .= '<tr><th>' . $label1 . '</th><td>' . $value1 . '</td></tr>';
}
if( isset($value2) ){
$html_output .= '<tr><th>' . $label2 . '</th><td>' . $value2 . '</td></tr>';
}
$html_output .= '</tr></tbody></table></div><br>';
// The CSS styling
$styles = '<style>
.discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 2px solid #e4e4e4; margin-bottom:8px;}
.discount-info table th, table.tracking-info td{ text-align: left; color: #737373; border: none; padding: 12px;}
.discount-info table td{ text-align: left; color: #737373; border: none; padding: 12px; }
</style>';
// The Output CSS + HTML
echo $styles . $html_output;
}
}
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
$value = wc_get_hearaboutus_options()[$hearaboutus];
echo '<p><strong>'.__('How did you hear about us?').'</strong> ' . $value . '</p>';
}
$whatsapp_list = $order->get_meta('_whatsapp_list');
$value = $whatsapp_list === 'Yes' ? 'Yes' : 'No';
echo '<p><strong>'.__('Added to WhatsApp list?').'</strong> ' . $value . '</p>';
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Проверено и работает.