在订单电子邮件和订单编辑页面中显示复选框字段数据
Display Checkbox Field Data in Order Email & Order Edit Page
一个月前 "" 答案代码帮助我保存并显示自定义结帐字段 (下拉菜单 'how did you hear about us'字段) 到订单电子邮件和订单编辑页面。
现在我需要添加另一个复选框来询问用户是否愿意注册 WhatsApp 列表。
我使用 some code 我发现创建这个新的结帐字段,尝试了几种不同的方法。但是我好像无法像上面一样让它出现在订单编辑页面&邮件中! (不显示 'array' 或根本不显示...)
代码如下:
// 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 文件。已测试并有效。
一个月前 "
现在我需要添加另一个复选框来询问用户是否愿意注册 WhatsApp 列表。
我使用 some code 我发现创建这个新的结帐字段,尝试了几种不同的方法。但是我好像无法像上面一样让它出现在订单编辑页面&邮件中! (不显示 'array' 或根本不显示...)
代码如下:
// 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 文件。已测试并有效。