一个月前,“Save and display order custom meta data in Woocommerce 3+”答案代码帮助我保存并在订单电子邮件和订单编辑页面中显示自定义结帐字段(下拉菜单“您是如何了解我们的字段”)。
现在,我需要添加另一个复选框,询问用户是否要注册WhatsApp列表。
我使用some code我发现创建这个新的结帐领域,尝试几种不同的方式。但我似乎无法以与上述相同的方式将其显示在订单编辑页面和电子邮件中! (没有显示'数组'或根本没有显示......)
这是代码:
// Add the WhatsApp Checkbox
function cw_custom_checkbox_fields( $checkout ) {
echo '<div class="cw_custom_class"><h2>'.__('Would you like to be added to the WhatsApp list?').'</h2>';
woocommerce_form_field( 'custom_checkbox', array(
'type' => 'checkbox',
'label' => __('Yes, add me to the list!'),
'required' => false,
), $checkout->get_value( 'custom_checkbox' ));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'cw_custom_checkbox_fields');
// Save the WhatsApp Data
add_action('woocommerce_checkout_update_order_meta', 'cw_checkout_order_meta');
function cw_checkout_order_meta( $order_id ) {
if ($_POST['custom_checkbox']) update_post_meta( $order_id, 'checkbox name', esc_attr($_POST['custom_checkbox']));
}
任何有助于在电子邮件和订单编辑页面中显示标签和价值的帮助将非常感谢!!
这里两个字段都按照管理员和电子邮件通知的方式合并,保存和显示:
// 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文件中。经过测试和工作。